I use the following code to convert Bitmap
to Uri
:
public Uri GetImageUriFunction(Context inContext, Bitmap inImage)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
But, when I run the code , the following exception occurs:
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=12433, uid=10438 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.createException(Parcel.java:1966)
at android.os.Parcel.readException(Parcel.java:1934)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1593)
at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:1014)
I have already added the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
to the manifest and enabled the Storage Permission in App Settings. How can I implement this grantUriPermission()
in the app?