【问题标题】:Android Read-Only File System ErrorAndroid 只读文件系统错误
【发布时间】:2012-03-21 05:47:10
【问题描述】:

您好,我的应用中有一个图库,当您单击缩略图时,它会在图像视图中显示图像。我正在为一个警报对话框设置一个长按监听器,该对话框有 2 个按钮,一个设置为墙纸,一个用于共享。我有共享意图和获取绘图缓存有点工作。它适用于模拟器,但不适用于我的手机。我已经使用了这个网站上的许多示例来实现这一点,但它根本不起作用。它不断强制关闭我手机上的应用程序。任何帮助表示赞赏。

                                alertDialog.setButton2("Share",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            imgView.setDrawingCacheEnabled(true);
                            Bitmap b = imgView.getDrawingCache();
                            File sdCard = Environment.getExternalStorageDirectory();
                            File file = new File(sdCard.getAbsolutePath() + "image.jpg");

                            try {
                                file.createNewFile();
                                OutputStream fos = null;
                                fos = new FileOutputStream(file);
                                b.compress(CompressFormat.JPEG, 100, fos);
                                fos.flush();
                                fos.close();
                            } catch (FileNotFoundException e) {

                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            Log.d("Save Example", "Image saved.");

                            Intent shareIntent = new Intent(Intent.ACTION_SEND);
                            Uri phototUri = Uri.parse("file:///sdcard/image.jpg");
                            shareIntent.setData(phototUri);
                            shareIntent.setType("image/jpeg");
                            shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
                            startActivity(Intent.createChooser(shareIntent, "Share Via"));
                        }

                    });

            alertDialog.show();
            return true;
        }
    });

更新:这似乎是一个问题

b.compress(CompressFormat.JPEG, 95, fos);

它一直说空指针。

原来空指针实际上是一个只读错误..所以它实际上并没有写入文件我得到一个 ioexception 只读文件系统。为什么要这样做?

【问题讨论】:

  • 在尝试写入之前需要查看目录的权限。您必须了解 UNIX 的基本文件系统权限才能进行此分析。
  • 您是否在清单文件中使用写权限??
  • 是的,我在清单中有 write.external.storage

标签: android bitmap imageview ioexception readonly


【解决方案1】:

好的,这就是我最终所做的,以便在其他人需要时分享图片。事实证明,我的 ImageView 没有被渲染为 BitMap,因此它制作的文件中没有任何内容。所以我只是从它的位置调用drawable并保存它。对于这个简单的画廊来说,它的代码更简洁。我在警报对话框中有它,但它也可以设置为常规按钮。

                alertDialog.setButton2("Share",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {

                            long currentTime = System.currentTimeMillis();
                            imgView.setDrawingCacheEnabled(true);
                            String newFolder = "/CFW";
                            String extStorageDirectory = Environment
                                    .getExternalStorageDirectory()
                                    .toString();
                            File sdCard = new File(extStorageDirectory
                                    + newFolder);
                            sdCard.mkdir();

                            File file = new File(sdCard.getAbsolutePath()
                                    + "/cfw" + currentTime + ".jpg");

                            try {
                                InputStream is = getResources()
                                        .openRawResource(pics[position]);
                                OutputStream os = new FileOutputStream(file);
                                byte[] data = new byte[is.available()];
                                is.read(data);
                                os.write(data);
                                is.close();
                                os.close();
                            } catch (FileNotFoundException e) {

                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            Log.d("Save Example", "Image saved.");

                            Intent shareIntent = new Intent(
                                    Intent.ACTION_SEND);
                            Uri photoUri = Uri
                                    .parse("file:///sdcard/CFW/cfw"
                                            + currentTime + ".jpg");
                            shareIntent.setData(photoUri);
                            shareIntent.setType("image/jpeg");
                            shareIntent.putExtra(Intent.EXTRA_STREAM,
                                    photoUri);
                            startActivity(Intent.createChooser(shareIntent,
                                    "Share Via"));
                        }

                    });

            alertDialog.show();
            return true;
        }
    });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2012-06-03
    • 2018-09-11
    相关资源
    最近更新 更多