2

My app creates picture files on external storage (/sdcard). I want those images to be viewable from Gallery (not to open them in Gallery, just make viewable if user switches to Gallery). Is there a way to achieve this?

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

3 Answers3

7

Use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); to get the path to the public images directory.

I have never tried to do so, but documentation says that that is the place where the public images (thus the gallery) are located.

Source: http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

EDIT: to notify the gallery DB that another file has been inserted, you might try the following:

private void addImageGallery( File file ) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}

Source:

https://stackoverflow.com/a/9096984/998759

Community
  • 1
  • 1
STT LCU
  • 4,348
  • 4
  • 29
  • 47
  • As a matter of fact, I do put images there by default, and they become visible, but only after device reboot. However, I want to keep the path customizeable. – Violet Giraffe Feb 20 '12 at 11:30
  • Thanks a lot, it works like charm! I've tried something similar before to no avail, but my code had a mistake, as I can see now :) – Violet Giraffe Feb 20 '12 at 15:23
2

It sounds like you need to use the MediaScannerConnection.

Jens
  • 16,853
  • 4
  • 55
  • 52
0

I agree with @Jens. Use MediaScannerConnection, particularly the static scanFile() method. See my answer here.

Community
  • 1
  • 1
darrenp
  • 4,265
  • 2
  • 26
  • 22