I want to allow users to upload images into my app from their gallery and then I will save their uri in my DB to keep track of upload. Right now I'm getting the Uri from uploaded images as referenced in this SO Answer:
Get file path of image on Android
The issue with this answer is it requires the image to be saved a second time.
String path = MediaStore.Images.Media.insertImage(context, img, "title, null);
I don't want to create duplicate images in a users gallery but I need to get the Uri from the image they upload. Any suggestions?
Code I'm using now to get the image 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);
}