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

本文实例为大家分享了 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实现拍照功能、android 拍照选择图片、android 调用拍照功能、android拍照功能、android 选择拍照,以便于您获取更多的相关知识。

时间: 2024-08-02 01:21:02

Android仿微信选择图片和拍照功能_Android的相关文章

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

本文实例为大家分享了 Android微信选择图片的具体代码,和微信拍照功能,供大家参考,具体内容如下 1.Android6.0系统,对于权限的使用都是需要申请,选择图片和拍照需要申请Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE这两个权限. if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageM

android仿微信聊天界面 语音录制功能_Android

本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图:       第一:chat.xml设计 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"

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

本文实例为大家分享了Android仿微信发表说说.心情功能,供大家参考,具体内容如下 既能实现拍照,选图库,多图案上传的案例,目前好多App都有类似微信朋友圈的功能,能过发表说说等附带图片上传.下面的就是实现该功能的过程:大家还没有看过Android Retrofit 2.0框架上传图片解决方案这篇文章,在看今天的就很容易,接在本项目中用到了一个library:photopicker,封装了图片的选择功能,是否选相机,还有选中图片后可以查看图片的功能.   一. 首先:将photopicker到

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

本文实例为大家分享了Android仿微信发表说说.心情功能,供大家参考,具体内容如下 既能实现拍照,选图库,多图案上传的案例,目前好多App都有类似微信朋友圈的功能,能过发表说说等附带图片上传.下面的就是实现该功能的过程:大家还没有看过Android Retrofit 2.0框架上传图片解决方案这篇文章,在看今天的就很容易,接在本项目中用到了一个library:photopicker,封装了图片的选择功能,是否选相机,还有选中图片后可以查看图片的功能. 一. 首先:将photopicker到工程

Android仿微信通讯录滑动快速定位功能

先给大家展示下效果图: 实现代码如下: 下面简单说下实现原理. public class IndexBar extends LinearLayout implements View.OnTouchListener { private static final String[] INDEXES = new String[]{"#", "A", "B", "C", "D", "E", &qu

Android仿微信发送语音消息的功能及示例代码

微信的发送语音是有一个向上取消的,我们使用onTouchListener来监听手势,然后做出相应的操作就行了. 直接上代码: //语音操作对象 private MediaPlayer mPlayer = null; private MediaRecorder mRecorder = null; //语音文件保存路径 private String FileName = null; FileName = Environment.getExternalStorageDirectory().getAbs

Android仿微信多图片选择,拍照等,适合快速导入到自己项目中

前段时间做项目需要用到图片选择,系统自带的intent方法选择图片已经过时了,不方便,所以就找到了一个大牛做的开源项目,拿过来修改了一下,添加了一些常用的功能,更加适合快速的集成到自己的项目中去,具体如下: 类似微信从相册中选择图片或者拍照: 可以控制单张或者多张选择: 支持预览,并且在预览页面可以直接删除图片: 选择图片后,在展示页面图片的右上角有删除按钮,点击可以直接删除: 更多属性可自行配置,适合快速集成到自己的项目中. 直接上图片 用到的三个基本方法 /** * 预览 * * @para

Android仿微信朋友圈图片查看器_Android

再看文章之前,希望大家先打开自己的微信点到朋友圈中去,仔细观察是不是发现朋友圈里的有个"九宫格"的图片区域,点击图片又会跳到图片的详细查看页面,并且支持图片的滑动和缩放?这个功能是不是很常用呢?!那么我今天正好做了这个Demo,下面为大家讲解一下.首先按照惯例先看一下效果图吧,尤其不会录制gif动画(哎~没办法,模拟器不支持多点触控,刚好我的手机又没有Root,不能录屏,悲催啊,大家见谅,想要看真实效果的话,烦请移到文章最下方转载文章中进行源码下载,点击下载源码,运行后再看效果哈~~)

Android仿UC浏览器左右上下滚动功能_Android

本文要解决在侧滑菜单右边加个文本框,并能实现文本的上下滑动和菜单的左右滚动.这里推荐可以好好看看android的触摸事件的分发机制,这里我就不详细讲了,我只讲讲这个应用.要实现的功能就像UC浏览器(或其它手机浏览器)的左右滚动,切换网页,上下滚动,拖动内容. 本文的效果:   一.功能要求与实现1.功能要求:(1)手指一开始按着屏幕左右移动时,只能左右滚动菜单,如果这时手指一直按着,而且上下移动了,那么菜单显示部分保持不变,但文本框也不上下移动!                       (2