【问题标题】:How to save bitmap as PNG to a specific folder in the Gallery?如何将位图作为 PNG 保存到图库中的特定文件夹?
【发布时间】: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


【解决方案1】:

它没有出现在图库中,因为您必须刷新图库,所以它知道新文件

   // Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
        new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ExternalStorage", "Scanned " + path + ":");
        Log.i("ExternalStorage", "-> uri=" + uri);
    }
});

对格式感到抱歉。我在我的手机上。

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2018-07-30
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多