2

i'm using the code specified here to store a Bitmap image and show it into the gallery.

Unfortunately that code let me store image to "Pictures" folder into the Gallery.

Is it possible to change the folder name with a name that i decide?

Community
  • 1
  • 1
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

1 Answers1

10

OK, I've modified the code from your link and tested this code. just use this:

public class CapturePhotoUtils {

    private static String folderName = "yourFolderName";

    /**
     * A copy of the Android internals  insertImage method, this method populates the
     * meta data with DATE_ADDED and DATE_TAKEN. This fixes a common problem where media
     * that is inserted manually gets saved at the end of the gallery (because date is not populated).
     *
     * @see android.provider.MediaStore.Images.Media#insertImage(ContentResolver, Bitmap, String, String)
     */
    public static final String insertImage(ContentResolver cr,
                                           Bitmap source,
                                           String title,
                                           String description) {

        String path = createDirectoryAndSaveFile(source, title);

        Uri uri;

        if (path != null) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, title);
            values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
            values.put(MediaStore.Images.Media.DESCRIPTION, description);
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            // Add the date meta data to ensure the image is added at the front of the gallery
            values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
            values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
            values.put(MediaStore.Images.Media.DATA, path);
            uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            return uri.toString();
        } else {
            return null;
        }

    }


    private static String createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

        File directory = new File(Environment.getExternalStorageDirectory() + File.separator + folderName);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        File file = new File(directory, fileName);
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(file);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }

}
Shalev Moyal
  • 644
  • 5
  • 12
  • I've edited code like you suggest but when trying to insert with contentResolver it says me: java.lang.IllegalArgumentException: Unknown URL /storage/emulated/0:MyAppGalleryFolder. I've tried to create it before like this File pictureDir = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "MyAppGalleryFolder"); if(pictureDir.exists() == false){ pictureDir.mkdir(); } but nothing changes – Jayyrus May 31 '16 at 06:31
  • I accidentally wrote pathSeparator instead of separator. I'v edited my comment, try it now. – Shalev Moyal May 31 '16 at 14:32
  • Nothing. I changed File.pathSeparator to File.separator after writed comment. Do ContentResolver check for content:// path? – Jayyrus May 31 '16 at 15:59
  • I've modified the code and tested. look at the edited answer. – Shalev Moyal May 31 '16 at 16:47
  • 1
    values.put(MediaStore.Images.Media.DATA, path); THIS is what I've been missing. Thank you! – Will Jones Nov 01 '18 at 13:59