Null Uri On Api Level >=23 Camera Intent
Solution 1:
You are using ACTION_IMAGE_CAPTURE
. ACTION_IMAGE_CAPTURE
is not supposed to return a Uri
via onActivityResult()
. Instead, either:
You provide
EXTRA_OUTPUT
on theACTION_IMAGE_CAPTURE
Intent
, in which case the image is supposed to be saved to that location (see this sample app), orYou do not provide
EXTRA_OUTPUT
on theACTION_IMAGE_CAPTURE
Intent
, in which case you get a thumbnailBitmap
back via the"data"
extra on theIntent
delivered toonActivityResult()
This is covered in the documentation.
Some buggy camera apps will return a Uri
. Do not count on this behavior, as well-written camera apps will not.
Solution 2:
privatestaticfinalintREQUEST_CODE_IMAGE=1000;
privateStringimportFileName="";
privatevoidcameraIntent() {
importFileName = getString(R.string.app_name) + newRandom().nextInt();
startActivityForResult(getCameraIntent(context, importFileName), REQUEST_CODE_IMAGE);
}
public Intent getCameraIntent(Context context, String fileName) {
IntentchooserIntent=null;
List<Intent> intentList = newArrayList<>();
IntenttakePhotoIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, context.getResources().getConfiguration().orientation);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile(context.getExternalCacheDir(), fileName)));
intentList = addIntentsToList(context, intentList, Collections.singletonList(takePhotoIntent));
if (intentList.size() > 0) {
Stringtitle= context.getResources().getString(R.string.choose);
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), title);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(newParcelable[]{}));
}
return chooserIntent;
}
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_IMAGE:
if (resultCode == RESULT_OK) {
onCaptureImageResult(data);
}
break;
}
}
privatevoidonCaptureImageResult(Intent data) {
Uri imageUri;
if (data == null) {
imageUri = Uri.fromFile(newFile(getExternalCacheDir(), importFileName));
} else {
imageUri = data.getData();
if (imageUri == null)
imageUri = Uri.fromFile(newFile(getExternalCacheDir(), importFileName));
}
}
If you get permissions (camera, read/write external storage permissions) from user, this code snippet will work correctly. You can call cameraIntent().
Solution 3:
Try this
Use both WRITE_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
to read and write file
<uses-featureandroid:name="android.hardware.camera2" /><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" />
Here ask to write permission & read permission add multiple permission
publicvoidrequestRuntimePermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(context,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
newString[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
}
Check this Android 6.0 multiple permissions
//Check permission granted or not if granted then call this block
UriselectedImage= data.getData();
String[] filePaths = {MediaStore.Images.Media.DATA};
Cursorc=this.getContentResolver().query(selectedImage, filePaths, null, null, null);
c.moveToFirst();
intcolumnIndex= c.getColumnIndex(filePaths[0]);
filePath = c.getString(columnIndex);
c.close();
Solution 4:
At least for older versions, if multiple selection is allowed (Intent#EXTRA_ALLOW_MULTIPLE
), URIs will be returned as clipboard data:
data.getClipData()
See Intent#getClipData()
and ClipData
.
Check if data is being returned there:
ClipDataclipData= data.getClipData();
UriselectedImage=null;
if (clipData != null) {
selectedImage = clipData.getItemAt(0).getUri();
} else {
selectedImage = data.getData();
}
Solution 5:
targetSdkVersion 21
change your targetsdk version in build.gradleto marshmallow it will be solved.
Post a Comment for "Null Uri On Api Level >=23 Camera Intent"