0

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);
}
Nate Schreiner
  • 759
  • 2
  • 7
  • 15
  • The question that you linked to is for `ACTION_IMAGE_CAPTURE`, which is not "from their gallery". What is your code that you are using to get an image? – CommonsWare Jun 10 '19 at 16:45
  • @CommonsWare Updated my question with my exact code. – Nate Schreiner Jun 10 '19 at 16:51
  • `Uri selectedImage = data.getData();` -- is this from `onActivityResult()`? If so, what is your `Intent` action? And, since `selectedImage` is a `Uri`, why can you not use that `Uri`? – CommonsWare Jun 10 '19 at 17:13

1 Answers1

0

I'd suggest posting the image to a storage bucket, like Amazon S3, Firebase Storage / Google Cloud Storage. This'll generate a URI / URL, and you could post that URI to your database.

I'm not sure if this entirely answers your question but I hope I might have pointed you somewhere in a good direction.

P.S.: Most of those storage solutions also have built-in APIs so you wouldn't be obligated to post the URI to the databse.

LoopsGod
  • 371
  • 2
  • 11