首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android只读文件系统错误

Android只读文件系统错误
EN

Stack Overflow用户
提问于 2012-03-21 13:47:11
回答 1查看 1.3K关注 0票数 0

嗨,我在我的应用程序中有一个画廊,当你点击缩略图时,它会在图像视图中显示图像。我正在设置一个警报对话框的Long Click监听器,它有两个按钮,一个设置为墙纸,另一个设置为共享。我有共享意图和get绘图缓存的一些工作。它可以在模拟器中工作,但不能在我的手机上工作。我已经在这个网站上使用了很多例子来实现这一点,但它根本不起作用。它会强制关闭我手机上的应用程序。任何帮助都是非常感谢的。

代码语言:javascript
复制
                                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;
        }
    });

更新:这似乎是一个问题

代码语言:javascript
复制
b.compress(CompressFormat.JPEG, 95, fos);

它一直在说空指针。

事实证明,空指针实际上是一个异常error..So,它并不是真的在写文件,我得到了一个只读文件系统。它为什么要这么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-23 13:46:12

好了,这就是我最终要做的,让它分享照片,以防其他人需要它。原来我的ImageView没有呈现为BitMap,所以它是make files,里面什么都没有。因此,我只是从它的位置调用drawable并保存它。对于这个简单的图库,它的代码要干净得多。我在一个警告对话框中有它,但它也可以设置为常规按钮。

代码语言:javascript
复制
                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;
        }
    });

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9799226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档