我正在尝试从位图中获取 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);
如果您已经在内存中拥有该图像,为什么您甚至需要对其在磁盘上的路径的引用?直接上传图片就好了
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句