Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an app image folder to show in Android gallery

I have an app where the user creates an image and then I want to save it so it's visible form the default gallery application.

Now I don't want the pictures to be saved in the same folder as the pictures taken from the camera, I want them to be saved in a folder dedicated to the app, just like images from apps like whatsapp or facebook.

I've tried saving them in this two locations:

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+ File.separator + appDirectoryName + File.separator);

and here

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appDirectoryName + File.separator);

If I browse through the phone I see that I successfully save the images but they don't show in the gallery app. It is obvious that I'm missing something but I don't know what it is. Maybe adding some kind of metadata to the files or folders so the gallery recognizes them?

like image 966
Nemesis Avatar asked Jan 21 '14 12:01

Nemesis


People also ask

Why images are not showing in gallery?

If your photos are visible in My Files but are not in the Gallery app, these files may be set as hidden. This prevents Gallery and other apps from scanning for media. To solve this, you can change the option for showing hidden files.


2 Answers

Well I found the answer in the end.

It turned out to be what I suspected. The saved image needs to have some metadata added in order to be seen in the gallery (at least in my device).

This is what I did:

OutputStream fOut = null;
    File file = new File(imagePath,"GE_"+ System.currentTimeMillis() +".jpg");

    try {
        fOut = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    try {
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    ContentValues values = new ContentValues();
    values.put(Images.Media.TITLE, this.getString(R.string.picture_title));
    values.put(Images.Media.DESCRIPTION, this.getString(R.string.picture_description));
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis ());
    values.put(Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
    values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
    values.put("_data", file.getAbsolutePath());

    ContentResolver cr = getContentResolver();
    cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
like image 144
Nemesis Avatar answered Sep 18 '22 12:09

Nemesis


I have had the same problem. The second way is the correct way, but you don't see the images in the Gallery because the gallery needs to be refreshed. So, you can wait a while until it refreshes itself, or you can use the MediaScanner - look here

Hope this helped!

like image 40
gilonm Avatar answered Sep 19 '22 12:09

gilonm