Android仿微信选择图片和拍照功能
作者:志向远大 时间:2023-08-18 05:22:50
本文实例为大家分享了 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,微信,图片,拍照
0
投稿
猜你喜欢
Spring Bean实例的创建及构造器的挑选
2021-08-02 09:35:57
Android文件下载功能实现代码
2023-08-14 00:58:55
C# Winform自动更新程序实例详解
2021-12-06 05:52:57
Java接口DAO模式代码原理及应用详解
2023-06-21 05:29:04
Java服务假死之生产事故的排查与优化问题
2022-01-12 04:03:37
Android自定义TitleView标题开发实例
2023-09-05 18:21:41
springboot中的静态资源加载顺序优先级
2023-08-24 11:12:31
Android 悬浮窗权限各机型各系统适配大全(总结)
2022-04-27 10:09:53
SpringBoot实现过滤器、拦截器与切片的实现和区别
2023-04-28 22:51:04
Java实现DFA算法对敏感词、广告词过滤功能示例
2023-02-11 10:24:33
浅析Java随机数与定时器
2022-06-04 16:21:10
Android实现IM多人员组合的群组头像
2023-05-24 06:39:54
Java 二分查找算法的实现
2022-07-23 11:10:13
Android自定义覆盖层控件 悬浮窗控件
2021-10-21 01:14:40
Java中SimpleDateFormat日期格式转换详解及代码示例
2023-09-04 22:13:43
java读取ftp中TXT文件的案例
2022-07-07 20:10:11
如何设计一个安全的API接口详解
2023-03-06 14:57:03
Android Studio设置或修改Android SDK路径方法
2023-11-07 17:22:10
用intellij Idea加载eclipse的maven项目全流程(图文)
2021-09-12 06:11:16
基于Java8 函数式接口理解及测试
2021-06-19 21:54:35