gpt4 book ai didi

android - ContentResolver 更改图库文件夹名称

转载 作者:行者123 更新时间:2023-11-29 14:30:34 28 4
gpt4 key购买 nike

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

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

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

最佳答案

好的,我已经修改了您链接中的代码并测试了这段代码。只需使用这个:

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

}

关于android - ContentResolver 更改图库文件夹名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37534124/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com