ContentResolver 更改图库文件夹名称
ContentResolver Change gallery folder name
我正在使用指定的代码 here 存储 Bitmap
图像并将其显示到图库中。
不幸的是,该代码让我可以将图像存储到 "Pictures" 文件夹中的图库中。
是否可以使用我决定的名称更改文件夹名称?
好的,我已经修改了您 link 的代码并测试了这段代码。只需使用这个:
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();
}
}
我正在使用指定的代码 here 存储 Bitmap
图像并将其显示到图库中。
不幸的是,该代码让我可以将图像存储到 "Pictures" 文件夹中的图库中。
是否可以使用我决定的名称更改文件夹名称?
好的,我已经修改了您 link 的代码并测试了这段代码。只需使用这个:
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();
}
}