从位图中获取uri



我正试图从位图中获取一个uri,以便将其上传到Firebase的存储中,但我的path一直返回为null。经过一些谷歌搜索,我尝试将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>添加到我的清单中以修复它,但没有任何效果。每当我在android studio中调试程序时,在创建path时,它似乎会崩溃。

这是代码:

private Uri getImageUri(Context inContext, Bitmap myImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), myImage, "Title", null);
return Uri.parse(path);
}

这是我如何使用方法的一个例子

Uri file = getImageUri(getApplicationContext(), uploadPhoto);
StorageReference picRef = storageRef.child("images/" + file.getLastPathSegment());
// Uploads the file
UploadTask uploadTask = picRef.putFile(file);

如果内存中已经有映像,为什么还要引用它在磁盘上的路径?只需直接上传图像

最新更新