Mediastore.images.media.insertimage Return Null Sometimes
I encountered strange problem, when calling MediaStore.Images.Media.insertImage, it returns null uri sometimes, but I found it can work well one or two times, I can't find it's rul
Solution 1:
I had a similar issue. If your device is using Marshmallow, and your target API is 23, you need to ask for permission (Manifest.permission.WRITE_EXTERNAL_STORAGE) at run time.
See also: https://medium.com/ribot-labs/exploring-the-new-android-permissions-model-ba1d5d6c0610#.y84j1nene
Solution 2:
onclick check for android version and permission as below:
@OverridepublicvoidonClick(View view)
{
intMyVersion= Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
checkIfAlreadyhavePermission();
}
Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
//Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile("/sdcard/1test.jpg")));
startActivityForResult(intent, 1);
}
and checkIfAlreadyhavePermission()
method body will be as below:
privatebooleancheckIfAlreadyhavePermission() {
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if((checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED))) {
//show dialog to ask permissionActivityCompat.requestPermissions(this,
newString[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.wRITE_EXTERNAL_STORAGE},
1);
returntrue;
} elsereturnfalse;
}
Post a Comment for "Mediastore.images.media.insertimage Return Null Sometimes"