【问题标题】:Saving a bitmap to disk/gallery in Android 4.3在 Android 4.3 中将位图保存到磁盘/图库
【发布时间】:2013-07-28 22:20:35
【问题描述】:

我一直在使用系统保存screenshots 的方式将我的位图保存到磁盘和图库中。这适用于 Android 4.2 及之前版本,但不适用于 Android 4.3。

相关代码:

Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream out = resolver.openOutputStream(uri);

完整代码here.

但是,在 4.3(新的 Nexus 7)中,我在第二行得到了 FileNotFoundException。我在website 上看不到 4.3 中与此相关的任何更改。

那么将图像保存到磁盘和图库的正确方法是什么?

已验证:

  • 存储已安装此method
  • imageUri 不为空(通常类似于“content://media/external/images/media/2034”)
  • 清单具有权限 android.permission.WRITE_EXTERNAL_STORAGE

【问题讨论】:

  • 你在用 HTC 测试吗?
  • 更新了 OP 以澄清这是在模拟器上。但是用户报告说 4.3 更新破坏了应用程序,我相信这是由于这个问题造成的。
  • 你的模拟器是否挂载了SD卡,我的意思是,碰巧忘记启用SD模拟? :) 如果不存在,您能否检查文件是否存在,创建临时文件并将其写入您从 ContentResolver 获得的输出流
  • 是的,我已经验证了存储是用这种方法挂载的pastebin.com/EbUJHnKK
  • FNF 异常消息是否显示为 “App op not allowed”?我在 4.3 中看到的唯一变化是他们处理文件权限的方式。我不知道为什么它会影响你,但我没有看到其他任何东西。您可以在此处查看可能更改的差异:github.com/android/platform_frameworks_base/commit/…

标签: android


【解决方案1】:

这是我将bitmaps 保存到图库的方式:

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri; //instantiate Uri with location of image
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);

【讨论】:

  • 这实际上与在画廊中存储 BITMAP 相关吗?我的意思是 MediaStore.Images.Media.insertImage (ContentResolver cr, Bitmap source, String title, String description)
  • @Yar,是的 - 这完全一样,只是方式不同 - 通过广播包含图像位置的给定 URI 应该添加到图库的意图。
  • 那么如果我有一个原始位图,我应该将它存储在 SD 卡上的某个位置,然后广播意图吗? Uri 设置为我的新文件?
  • @Yar 是的,这正是你应该做的。
【解决方案2】:

在您的清单文件中尝试将目标 sdk 更改为 18.-

  <uses-sdk android:minSdkVersion="7" 
    android:targetSdkVersion="18"/>

它可能会解决您的问题(可能不会)。在 4.3 JELLY_BEAN_MR2 中,android 进行了一些更改,并且 android 清楚地写道 您的应用可能在受限配置文件环境中行为不端。所以请看http://developer.android.com/about/versions/android-4.3.html

【讨论】:

    【解决方案3】:

    我的清单中有这些权限。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    

    但我使用的是 Target SDK 版本 15。是否要求您必须使用目标 SDK 18?

    顺便说一句:

    这是从 Facebook 下载个人资料图片的示例代码:

    private class DownloadProfilePicTask extends AsyncTask<Void,String,String> {
        Bitmap profilePic;
        String fileName;
        String id;
        String type;
        URL img_value;
        public DownloadProfilePicTask(String i,String ty)
        {
            id = i;
            if(id==null)
            {
                //Log.v("Id is null", "Error");
            }
            //Log.v("Download Profile Pic Task initialized for id:",id);
            type = ty;
        }
        @Override
        protected String doInBackground(Void...param) {
             String root = Environment.getExternalStorageDirectory().toString();
             if(root==null)
             {
                 return null;
             }
             try{
             profilePic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
             }
             catch (IOException e) {
                 e.printStackTrace();
    
             }
    
            if(profilePic == null)
            {
                //Log.v("profilePic is null", "Error");
            }
             //Log.v("Root for saving images",root );
             File myDir = new File(root + "/saved_images");
             myDir.mkdirs();
            fileName = root + "/saved_images/" + id + ".png";
            //Log.v("filename is ",fileName);
             File file = new File (fileName); 
             fileName = file.getPath();
             if (file.exists ()) file.delete (); 
             try {
                    FileOutputStream out = new FileOutputStream(file);
                    profilePic.compress(Bitmap.CompressFormat.PNG, 90, out);
                    out.flush();
                    out.close();
             } catch (Exception e) {
                    e.printStackTrace();
             }
             return id;
        }
        @Override
        protected void onPreExecute()
        {
            try
            {
                img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=" + type);
            }
             catch (MalformedURLException e) {
                    e.printStackTrace();
    
            } 
    
        }
        @Override
        protected void onPostExecute(String result) {
        }
    }
    

    然后我就打电话:

    new DownloadProfilePicTask(id,type).execute();
    

    下载并自动保存图像。

    注意:您将不得不稍微使用 filePath 来确定确切的位置。

    【讨论】:

      【解决方案4】:

      为了避免开发,Android 4.3 上的文件系统发生了一些变化。直接写入“/sdcard”或“/mnt/sdcard”但使用android ExternalStorage系统。 (http://source.android.com/devices/tech/storage/index.html)

      注意: ExternalStorage 可以是内部存储器 :p

      对于您的情况,您是否尝试过使用基于 getExternalStorage 的方法? (像这样:Find an external SD card location)

      【讨论】:

        猜你喜欢
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        相关资源
        最近更新 更多