Android仿微信选择图片和拍照功能

所属分类: 软件编程 / Android 阅读数: 20
收藏 0 赞 0 分享

本文实例为大家分享了 Android微信选择图片的具体代码,和微信拍照功能,供大家参考,具体内容如下

1.Android6.0系统,对于权限的使用都是需要申请,选择图片和拍照需要申请Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE这两个权限。

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
   ActivityCompat.requestPermissions((Activity) this,
     new String[] { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
     REQUEST_STORAGE_READ_ACCESS_PERMISSION);
  }

2.通过图片选择器MultiImageSelector来管理: 选择模式、最大选择数量、是否启动相机等功能。

3.点击图片选择按钮跳转到MultiImageSelectorActivity类,其布局如下:(一个Toobar + 一个FrameLayout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:background="#181819"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/mis_actionbar_color"
  app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  android:minHeight="?android:attr/actionBarSize">

  <Button
   android:id="@+id/commit"
   android:background="@drawable/mis_action_btn"
   android:minHeight="1dp"
   android:minWidth="1dp"
   android:layout_marginRight="16dp"
   android:paddingLeft="10dp"
   android:paddingRight="10dp"
   android:paddingTop="5dp"
   android:paddingBottom="5dp"
   android:textColor="@color/mis_default_text_color"
   android:textSize="14sp"
   android:layout_gravity="right"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

 </android.support.v7.widget.Toolbar>

 <FrameLayout
  android:id="@+id/image_grid"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

</LinearLayout>

4.调用如下方法填充展示图片的fragment(MultiImageSelectorFragment)。

   getSupportFragmentManager().beginTransaction()
     .add(R.id.image_grid, Fragment.instantiate(this, MultiImageSelectorFragment.class.getName(), bundle))
     .commit();

5.MultiImageSelectorFragment布局用gridview显示从相册获取的图片

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:background="@android:color/black"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <GridView
  android:id="@+id/grid"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:horizontalSpacing="@dimen/mis_space_size"
  android:verticalSpacing="@dimen/mis_space_size"
  android:paddingBottom="?android:attr/actionBarSize"
  android:clipToPadding="false"
  android:numColumns="3"/>

 <RelativeLayout
  android:clickable="true"
  android:id="@+id/footer"
  android:background="#cc000000"
  android:layout_alignParentBottom="true"
  android:layout_width="match_parent"
  android:layout_height="?android:attr/actionBarSize">

  <Button
   android:id="@+id/category_btn"
   android:paddingLeft="16dp"
   android:paddingRight="16dp"
   android:layout_centerVertical="true"
   android:textColor="@color/mis_folder_text_color"
   tools:text="所有图片"
   android:textSize="16sp"
   android:gravity="center_vertical"
   android:drawableRight="@drawable/mis_text_indicator"
   android:drawablePadding="5dp"
   android:background="@null"
   android:singleLine="true"
   android:ellipsize="end"
   android:layout_width="wrap_content"
   android:layout_height="match_parent" />

  </RelativeLayout>

</RelativeLayout>

6调用android.support.v4.app.LoaderManager.class类里面的LoaderCallbacks方法,等加载完成后给mImageAdapter设置数据。

mImageAdapter.setData(images);

7.当允许拍照的时候,显示拍照按钮,调用系统相机功能。

 mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    if (mImageAdapter.isShowCamera()) {
     if (i == 0) {
      showCameraAction();
     } else {
      Image image = (Image) adapterView.getAdapter().getItem(i);
      selectImageFromGrid(image, mode);
     }
    } else {
     Image image = (Image) adapterView.getAdapter().getItem(i);
     selectImageFromGrid(image, mode);
    }
   }
  });

调用相机功能

 /**
  * Open camera
  */
 private void showCameraAction() {
  if(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED){
   requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
     getString(R.string.mis_permission_rationale_write_storage),
     REQUEST_STORAGE_WRITE_ACCESS_PERMISSION);
  }else {
   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
    try {
     mTmpFile = FileUtils.createTmpFile(getActivity());
    } catch (IOException e) {
     e.printStackTrace();
    }
    if (mTmpFile != null && mTmpFile.exists()) {
     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
     startActivityForResult(intent, REQUEST_CAMERA);
    } else {
     Toast.makeText(getActivity(), R.string.mis_error_image_not_exist, Toast.LENGTH_SHORT).show();
    }
   } else {
    Toast.makeText(getActivity(), R.string.mis_msg_no_camera, Toast.LENGTH_SHORT).show();
   }
  }
 }

选择图片

 /**
  * notify callback
  * @param image image data
  */
 private void selectImageFromGrid(Image image, int mode) {
  if(image != null) {
   if(mode == MODE_MULTI) {
    if (resultList.contains(image.path)) {
     resultList.remove(image.path);
     if (mCallback != null) {
      mCallback.onImageUnselected(image.path);
     }
    } else {
     if(selectImageCount() == resultList.size()){
      Toast.makeText(getActivity(), R.string.mis_msg_amount_limit, Toast.LENGTH_SHORT).show();
      return;
     }
     resultList.add(image.path);
     if (mCallback != null) {
      mCallback.onImageSelected(image.path);
     }
    }
    mImageAdapter.select(image);
   }else if(mode == MODE_SINGLE){
    if(mCallback != null){
     mCallback.onSingleImageSelected(image.path);
    }
   }
  }
 }

本文已被整理到了《Android微信开发教程汇总》,欢迎大家学习阅读。

源码下载:http://xiazai.jb51.net/201611/yuanma/AndroidselectPicture(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多