【问题标题】:ContentResolver Change gallery folder nameContentResolver 更改图库文件夹名称
【发布时间】:2016-09-28 18:47:20
【问题描述】:

我正在使用here 指定的代码来存储Bitmap 图像并将其显示到图库中。

不幸的是,该代码让我将图像存储到图库中的“图片”文件夹中。

是否可以使用我决定的名称更改文件夹名称?

【问题讨论】:

    标签: android image gallery android-contentresolver


    【解决方案1】:

    好的,我已经修改了您链接中的代码并测试了此代码。就用这个:

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

    【讨论】:

    • 我已经按照您的建议编辑了代码,但是当尝试使用 contentResolver 插入时,它说我:java.lang.IllegalArgumentException: Unknown URL /storage/emulated/0:MyAppGalleryFolder。我之前尝试过像这样创建它 File pictureDir = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "MyAppGalleryFolder"); if(pictureDir.exists() == false){ pictureDir.mkdir(); } 但没有任何变化
    • 我不小心写了 pathSeparator 而不是 separator。我已经编辑了我的评论,现在就试试吧。
    • 没什么。写评论后,我将 File.pathSeparator 更改为 File.separator。 ContentResolver 是否检查 content:// 路径?
    • 我已经修改了代码并进行了测试。查看编辑后的答案。
    • values.put(MediaStore.Images.Media.DATA, path);这就是我一直想念的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 2013-04-04
    • 2020-01-17
    相关资源
    最近更新 更多