简体   繁体   中英

In Nougat Capture Image from Camera and get file from Uri

In Android Nougat and above versions the method to capture image from camera intent is changed and following code is working fine for me.

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                   // File file = new File(AppGlobal.URI_CAPTURED_IMAGE.getPath());

                                    AppGlobal.URI_CAPTURED_IMAGE = FileProvider.getUriForFile(parent, context.getPackageName() + ".provider", AppGlobal.getOutputMediaFile());
                                }
                                else
                                {
                                    AppGlobal.URI_CAPTURED_IMAGE = Uri.fromFile(AppGlobal.getOutputMediaFile());
                                }
                                camera.putExtra(MediaStore.EXTRA_OUTPUT, AppGlobal.URI_CAPTURED_IMAGE);
                                camera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                startActivityForResult(camera, WritePostFragment.REQUEST_CODE_PHOTO_CAPTURE);

In onActivityResult method I'm able to show the captured image in ImageViews as well.

Glide.with(parent).load(AppGlobal.URI_CAPTURED_IMAGE).centerCrop().into(profilePic_iv);

But when i use the same uri to get File then it says no file exists at given path. What could be the issue? How can i parse the Uri to get File. It seems to be version related stuff.

Sample uri is as follows:

content://com.example.demo.provider/external_files/Camera/IMG_20171213_015646.jpg

How can i parse the Uri to get File

You don't. Moreover, you do not need to. Your file is whatever AppGlobal.getOutputMediaFile() returns. You need to hold onto that value (including putting it in the saved instance state Bundle , in case your process is terminated while the camera app is in the foreground). See this sample app for how to use ACTION_IMAGE_CAPTURE with FileProvider .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM