Android7.0拍照选取图片裁剪

Android拍照和选取图片之前需要先获取对应的权限。
需要了解的请看 Android6.0权限管理
下面默认已经获取权限。
测试机型:
vivo V3Max A android 5.1.1
Honor NOTE 8(型号:EDI-AL10) android 7.0

适配Android7.0。

Uri.fromFile() 方法被 FileProvider.getUriForFile() 代替。

第一步:再清单文件中配置provider。

            
        
第二步:在res目录下新建xml目录,在xml目录下新建file_paths.xml文件。(名字可以随便起,只要和清单文件中配置的一样就行),配置为path=""意思是将整个根目录给共享出去,如果设置为path="/abc"就只能获取/adc目录下文件的uri了。


    
        
        
        
        
        
        
    

第三步:在需要文件Uri的地方这样调用
 public static Uri getUriForN(Context context, File file) {
        return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
    }
准备
public class AppUtil {
    //存放照片的目录
    public static String getImageRootPath() {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/takephoto";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }
}

拍照

调用PhotoUtil拍照方法
private File photoFile;
photoFile = new File(AppUtil.getImageRootPath(), System.currentTimeMillis() + "camer.jpg");
PhotoUtil.takePhoto(context, photoFile, requestCode);
PhotoUtil
public static void takePhoto(Context context, File outPutFile, int requestCode) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {// >=7.0
            Uri outputUri = getUriForN(context, outPutFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outPutFile));
        }
        ((Activity) context).startActivityForResult(intent, requestCode);
    }

从相册选择

调用PhotoUtil从相册选择方法
PhotoUtil.choosePhoto(context, requestCode);
PhotoUtil
public static void choosePhoto(Context context, int requestCode) {
        //用这种方法就不用区分>=19来获取路径了
//        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//        ((Activity) context).startActivityForResult(intent, requestCode);
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        ((Activity) context).startActivityForResult(intent, requestCode);
    }

onActivityResult 回调

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_TAKE_PHOTO) {//拍照
            if (resultCode == Activity.RESULT_OK) {
                photoUtil.zoomPhoto(this, photoFile, zoomFile, REQUEST_CODE_ZOOM_PHOTO);
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.e(TAG, "拍照已取消");
            }
        } else if (requestCode == REQUEST_CODE_CHOOSE_PHOTO) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    Uri data_choose_photo = data.getData();
                    String path = photoUtil.getPath(this, data_choose_photo);
                    photoUtil.zoomPhoto(this, new File(path), zoomFile, REQUEST_CODE_ZOOM_PHOTO);
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.e(TAG, "选择照片已取消");
            }
        } else if (requestCode == REQUEST_CODE_ZOOM_PHOTO) {
            if (resultCode == Activity.RESULT_OK) {
                Bitmap bitmap = BitmapFactory.decodeFile(zoomFile.getAbsolutePath());
                iv.setImageBitmap(bitmap);
            } else if (requestCode == Activity.RESULT_CANCELED) {
                Log.e(TAG, "裁剪照片已取消");
            }
        }
    }

针对图库得到图片路径

 //针对图库得到图片路径
    public static String getPath(Context context, Uri uri) {
        String imagePath = null;
        if (Build.VERSION.SDK_INT >= 19) {
            if (DocumentsContract.isDocumentUri(context, uri)) {
                String docId = DocumentsContract.getDocumentId(uri);

                if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                    String id = docId.split(":")[1];
                    String selection = MediaStore.Images.Media._ID + "=" + id;
                    imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
                } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                    Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                    imagePath = getImagePath(context, contentUri, null);
                }
            } else if (ContentResolver.SCHEME_CONTENT.equalsIgnoreCase(uri.getScheme())) {
                imagePath = getImagePath(context, uri, null);
            } else if ( ContentResolver.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) {
                imagePath = uri.getPath();
            }
        } else {
            imagePath = getImagePath(context, uri, null);
        }
        return imagePath;
    }

    private static String getImagePath(Context context, Uri uri, String selection) {
        String Path = null;
        Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                Path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return Path;
    }

如有错误欢迎补充和指正~~

你可能感兴趣的:(Android7.0拍照选取图片裁剪)