Jayyrus
Jayyrus

Reputation: 13061

ContentResolver Change gallery folder name

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?

Upvotes: 2

Views: 2259

Answers (1)

Shalev Moyal
Shalev Moyal

Reputation: 644

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();
    }

}

Upvotes: 10

Related Questions