我想允许用户从图库中将图像上传到我的应用程序中,然后将其uri保存在数据库中以跟踪上传。现在,我正在从SO答案中引用的上传图像中获取Uri:
Get file path of image on Android
此答案的问题是它要求第二次保存图像。
String path = MediaStore.Images.Media.insertImage(context, img, "title, null);
我不想在用户库中创建重复的图像,但是我需要从他们上传的图像中获取Uri。有什么建议吗?
我现在用来获取图像Uri的代码:
Uri selectedImage = data.getData();
Log.d(TAG, "Imediate Uri: " + selectedImage);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
Uri tempUri = getImageUri(getApplicationContext(), bitmap);
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
答案 0 :(得分:0)
我建议将图像发布到存储桶,例如Amazon S3,Firebase Storage / Google Cloud Storage。这将生成一个URI / URL,您可以将该URI发布到数据库中。
我不确定这是否能完全回答您的问题,但我希望我已为您指明了正确的方向。
P.S .:大多数存储解决方案还具有内置API,因此您不必将URI发布到数据库中。