简体   繁体   中英

Convert a bitmap to uri

I´m trying convert bitmap to uri and I have a problem with the SecurityException, any idea for resolve?

public Uri getImageUri(Context ctx, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage
            (ctx.getContentResolver(),
            bitmap, "Temp", null);
    return Uri.parse(path);
}

The exception I'm getting is:

E/MediaStore: Failed to insert image
    java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=10963, uid=10274 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
        at android.os.Parcel.readException(Parcel.java:1693)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)

Go to AndroidManifest file in your project and add this line

<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <<=THIS
.
.
<application></application>
.
.
</manifest>

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