将位图转换为uri

时间:2019-07-23 01:13:03

标签: java android bitmap uri

我正在尝试将位图转换为uri,并且SecurityException出现问题,可以解决吗?

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);
}

我得到的例外是:

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)

1 个答案:

答案 0 :(得分:0)

转到项目中的AndroidManifest文件并添加此行

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