【发布时间】:2016-11-30 20:34:47
【问题描述】:
鉴于我想将位图保存到手机公共图库中的特殊文件夹中,我尝试了以下操作:
public static void saveToGallery(Context context, Bitmap bitmap) {
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), File.separator + "FolderForMyApp");
path.mkdirs();
File imageFile = new File(path, "image_name.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
catch (IOException e) {
Log.w(TAG, "Could not resolve file stream: " + e);
}
}
但它实际上并没有出现在图库中。我做错了什么?
【问题讨论】:
-
@CommonsWare 那么添加
MediaScannerConnection.scanFile(context, new String[]{imageFile.getAbsolutePath()}, new String[]{"image/png"}, null);这一行是否正确? -
应该可以。请注意,这项工作是异步的,无论是在更新
MediaStore方面,然后任何给定的画廊类型应用程序都必须根据MediaStore更改更新其UI。 -
@CommonsWare 我想这就是第三个参数的用途(MediaStore 更新时的回调函数)?
-
I want to save to a special folder in the public Gallery on the phone,。你说话的方式!您不能将文件存储到画廊,因为画廊应用程序是一个应用程序,没有存储空间。您只能将文件保存到外部和可移动存储上的特定文件夹。然后恰好有一个媒体存储索引所有这些媒体文件。还有一个图库应用,它使用这个索引来展示你的照片。
标签: android file-io bitmap filesystems gallery