android选择系统图库、拍照裁剪,适配Android6.0、7.0、8.0

1.选择系统图库

a.第一种

Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
activity.startActivityForResult(intent, CHOOSE_PHOTO);

有些三星手机使用这种方法选择图片并且调用系统裁剪会崩溃抛权限问题(SecurityException:UID 10292 does nnot have permission to content://com.google.android.apps.photos.contentprovider....),可以采用第二种方法:

b.第二种

Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
   intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
 } else {
   intent.setAction(Intent.ACTION_GET_CONTENT);
 }
activity.startActivityForResult(intent, CHOOSE_PHOTO);

2.系统拍照(Android 7.0 就是 File 路径的变更,需要使用 FileProvider 来做,下面看拍照的代码。)

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
/*获取当前系统的android版本号*/
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Uri uri;
File picture = new File(photoTemp);
if (currentapiVersion < 24) {
	uri = Uri.fromFile(picture);
} else {
	if (fragment!=null) {
		uri = FileProvider.getUriForFile(fragment.getActivity(), fragment.getActivity().getPackageName() + ".fileprovider", picture);
	}else{
		uri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", picture);
	}
	intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
	intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
	intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
	activity.startActivityForResult(intent, CAMERA);



在AndroidManifest.xml的application标签里添加
 <provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="包名.fileprovider"
      android:exported="false"
      android:grantUriPermissions="true">
      <meta-data
           android:name="android.support.FILE_PROVIDER_PATHS"
           android:resource="@xml/filepaths"/>
 </provider>

在res文件下创建xml文件目标添加filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!-- 第一个path是我用来解决7.0权限的,path需要临时授权访问的路径(.代表所有路径) -->
    <external-path path="." name="camera_photos" />
    <!-- 这个下载路径也不可以修改,必须为GDTDOWNLOAD -->
    <external-path name="gdt_sdk_download_path" path="GDTDOWNLOAD" />
</paths>

3.系统裁剪:

File picture = new File(photoTemp);
Uri uri = Uri.fromFile(picture);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 24) {
    uri = FileProvider.getUriForFile(activity, getPackageName() + ".fileprovider", picture);
}

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
if (Build.VERSION.SDK_INT >= 24) {
    // android7.0设置输出文件的uri //添加这一句表示对目标应用临时授权该Uri所代表的文件
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("return-data", false);
File picture = new File(photoTemp);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picture));//图像输出
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 图片格式
activity.startActivityForResult(intent, ZOOM_RESULT);

裁剪完回调:

File picture = new File(photoTemp);
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.fromFile(picture )));

 

图片缓存删除不掉问题:方法一

File picture = new File(photoTemp);
if (picture.exists()) {
   picture.delete();
   updateFileFromDatabase(activity==null?fragment.getContext():activity,picTemp);
}
public static void updateFileFromDatabase(Context context, File file) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            String[] paths = new String[]{Environment.getExternalStorageDirectory().toString()};
            MediaScannerConnection.scanFile(context, paths, null, null);
            MediaScannerConnection.scanFile(context, new String[]{
                            file.getAbsolutePath()},
                    null, new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                        }
                    });
        } else {
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
    }

方法二:每次拍照存的图片文件名字不一样加个时间戳

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值