How to pick image for crop from camera or gallery in Android 7.0?

后端 未结 9 2258
礼貌的吻别
礼貌的吻别 2020-12-01 04:56

Pick image for crop from gallery and camera it\'s done for below Android 7.0 but in Android Nought it crashes in camera. I use fileprovider for it but doesn\'t work.

9条回答
  •  醉梦人生
    2020-12-01 05:37

    I just solve this problem on Nexus6p android N,you need grant permission to uri so the system camera can access the file wait crop temporarily,because of StrictMode Android N has do not support pass a file:Uri in an Intent extra anymore see Scheme Ban in N Developer Preview,we use FileProvider instead.here is my source code:

    AndroidManifest.xml

    
            
    
    

    filepaths.xml

    
    
        
    
    

    MainActivity.java

    Uri photoURI = FileProvider.getUriForFile(context, "dreamgo.corp.provider", file);
    //grant uri with essential permission the first arg is the The packagename you would like to allow to access the Uri.
    context.grantUriPermission("com.android.camera",photoURI,
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(photoURI, "image/*");
    
    //you must setup two line below
    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", true);
    //you must setup this
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
    startActivityForResult(intent, 1);
    

提交回复
热议问题