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标签里添加
 
      
 

在res文件下创建xml文件目标添加filepaths.xml


    
    
    
    

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())));
        }
    }

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

你可能感兴趣的:(笔记)