Monday 6 February 2017

How to pick image from camera or gallery in Android 7.0 ? (Intent-FileProvider) (FileNotFoundException )

file:// scheme is now not allowed to be attached with Intent on targetSdkVersion 24.




put on mainifaest file 



 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>


this has to be there in your manifest.. for effect in the naugut android 7.0.
Again you need to add provider_paths.xml file in xml.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

//take a photo from gallery
public void gallery() {
    //set UUID to filename
    String PHOTO_FILE_NAME = UUID.randomUUID().toString()+".jpg";
    Utils.putValue(this, Constants.UserPortraitFilePath,PHOTO_FILE_NAME);
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
  //take a photo from camera
public void camera() {
    //check sdcard is usable or not
    if (Utils.hasSdcard()) {
        //set UUID to filename
        String PHOTO_FILE_NAME = UUID.randomUUID().toString()+".jpg";
        Utils.putValue(this,Constants.UserPortraitFilePath,PHOTO_FILE_NAME);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        //set file location to DreamGo/Image
        File path = Environment.getExternalStorageDirectory();
        File dir = new File(path, "DreamGo/Image");
        if(!dir.exists())
            dir.mkdirs();
        //Android N need use FileProvider get file 
        //uri because StrictMode System
        //getUriForFile(content,provider author,file)
        Uri photoURI = FileProvider.getUriForFile(context, "dream.go.provider",
                new File(dir.getAbsolutePath(), PHOTO_FILE_NAME));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
    }else {
        showToast("no storage device");
    }
}

private static final int PHOTO_REQUEST_CAMERA = 0;//camera
private static final int PHOTO_REQUEST_GALLERY = 1;//gallery
private static final int PHOTO_REQUEST_CUT = 2;//image crop
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String PHOTO_FILE_NAME = Utils.getValue(this, Constants.UserPortraitFilePath);
    File path = Environment.getExternalStorageDirectory();
    File dir = new File(path, "DreamGo/Image");
    if(!dir.exists())
        dir.mkdirs();
    switch (requestCode)
    {
        case PHOTO_REQUEST_GALLERY:
            if (data != null){
                //file from gallery
                File sourceFile = new File(getRealPathFromURI(data.getData()));
                //blank file DreamGo/Image/uuid.jpg
                File destFile = new File(dir.getAbsolutePath(), PHOTO_FILE_NAME);
                Log.e("photo",data.getData().getPath());
                try {
                    //copy file from gallery to DreamGo/Image/uuid.jpg
                    // otherwise crop method can't cut image without write permission
                    copyFile(sourceFile,destFile);
                    //Android N need use FileProvider to get file uri
                    Uri photoURI = FileProvider.getUriForFile(context, "dream.go.provider", destFile);
                    //cut image
                    crop(photoURI);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } 
}

Refer URL :- http://stackoverflow.com/questions/39313752/how-to-pick-image-for-crop-from-camera-or-gallery-in-android-7-0

3 comments: