Adapter 7.0, Android open an album, camera, crop usage, details of the deal

Android 7.0 on the permissions required to do the adaptation

After Android 7.0 have been restricted to read file operations, if you do not deal with an application to obtain file: URI // format, then the application will throw FileUriExposedException. Then we need to use Fileprovider, on the usage of Fileprovider reference this article
  Android 7.0 Uri adaptation:

Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
   uri = FileProvider.getUriForFile(context, "com.hjl.android7.fileprovider", file);
} else {
   uri = Uri.fromFile(file);
}

Call system camera key codes

imageUri: the need to pass the adapted Uri Uri save the photo camera , you can call this method directly in the Activity inside, and onActivityResult manipulate photos in Uri callback method

public static void takePicture(Activity activity, Uri imageUri, int requestCode) {
   //调用系统相机  imageUri: 需要传入适配后的Uri 这个Uri 保存拍照后的照片 
   Intent intentCamera = new Intent();
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
     intentCamera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     //添加这一句表示对目标应用临时授权该Uri所代表的文件
   }
   intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
   
    //将拍照结果保存至photo_file的Uri中,不保留在相册中 
   intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
   if (activity!=null){
      activity.startActivityForResult(intentCamera, requestCode);
   }
}

Call system camera key codes

public static void openPic(Activity activity, int requestCode) {
     Intent albumIntent = new Intent(Intent.ACTION_PICK, null);
     albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");  
     activity.startActivityForResult(albumIntent, requestCode);
 }

In onActivityResult get selected pictures Uri callback method:

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Uri uri = data.getData();
        ... //拿到uri后就可以进行相应的处理了
}

Uri can get after the appropriate treatment, for example, and converted into a bitmap object is cached locally:

File file = new File(FileUtils.getSDCachePath(),"test.png");
//缓存文件路径
try {
     OutputStream fos = new FileOutputStream(file);
     Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream( data.getData() ));
     bitmap.compress(Bitmap.CompressFormat.PNG,100,fos);
     fos.flush();
     fos.close();
} catch (FileNotFoundException e) {
     e.printStackTrace();
}

Crop images critical code

/**
* @param activity 当前activity
* @param orgUri 剪裁原图的Uri
* @param desUri 剪裁后的图片的Uri
* @param aspectX X方向的比例
* @param aspectY Y方向的比例
* @param width 剪裁图片的宽度
* @param height 剪裁图片高度
* @param requestCode 剪裁图片的请求码
*/
public static void cropImageUri(Activity activity, Uri orgUri, Uri desUri, int aspectX, int aspectY, int width, int height, int requestCode) {
   Intent intent = new Intent("com.android.camera.action.CROP");
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
   }
   intent.setDataAndType(orgUri, "image/*");
   intent.putExtra("crop", "true");
   intent.putExtra("aspectX", aspectX);
   intent.putExtra("aspectY", aspectY);
   intent.putExtra("outputX", width);
   intent.putExtra("outputY", height);
   intent.putExtra("scale", true);
   //将剪切的图片保存到目标Uri中
   intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);
   intent.putExtra("return-data", false);
   intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
   intent.putExtra("noFaceDetection", true);
   intent.putExtra("scaleUpIfNeeded", true);//去除黑边
   activity.startActivityForResult(intent, requestCode);
}

note:

  • The first pit:
    desUri after save the cropped picture: This desUri to use Uri.fromFile (file) generated, instead of using FileProvider.getUriForFile, otherwise they will be displayed: "Can not save the cropped picture"
    and set intent.putExtra (MediaStore.EXTRA_OUTPUT, desUri); after Uri output in the onActivityResult () obtained by data.getData in Uri will be empty
  • The second pit:
    If the cropped image pixel is too low, there will be black bars, the solution: add the following two sentences above methods clipping call
裁剪图片后出现黑边:
intent.putExtra("scale", true);//去除黑边
intent.putExtra("scaleUpIfNeeded", true);//去除黑边

Annex: Android local cache file of each path Summary

Android local cache file each path Summary

Reference article

Published 27 original articles · won praise 6 · views 1667

Guess you like

Origin blog.csdn.net/weixin_41802023/article/details/90602450