[英]Getting a uri from a bitmap
I'm trying to get a uri from a bitmap so that I can upload it to Firebase's storage, but my path
keeps coming back as null.我正在尝试从 bitmap 获取 uri,以便我可以将其上传到 Firebase 的存储,但我的path
一直返回为 null。 After some googling, I tried adding <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
to my manifest to fix it, but it didn't have any effect.经过一番谷歌搜索后,我尝试将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
添加到我的清单中以修复它,但它没有任何效果。 Whenever I debug my program in android studio, it seems to break when creating path
.每当我在 android 工作室调试我的程序时,它似乎在创建path
时中断。
Here's the code:这是代码:
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);
}
This an example of how I am using the method这是我如何使用该方法的示例
Uri file = getImageUri(getApplicationContext(), uploadPhoto);
StorageReference picRef = storageRef.child("images/" + file.getLastPathSegment());
// Uploads the file
UploadTask uploadTask = picRef.putFile(file);
If you already have the image in memory, why do you even need a reference to its path on disk?如果您已经拥有 memory 中的图像,为什么还需要对其在磁盘上的路径的引用? Just directly upload the image直接上传图片就好了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.