Android只读文件系统错误
问题描述:
嗨我在我的应用程序中有一个画廊,当您单击缩略图时在图像视图中显示图像。我正在为一个警告对话框设置一个长单击侦听器,该对话框有2个按钮,一个用于设置壁纸,另一个用于分享。我有共享意图和获取图形缓存有点工作。它适用于模拟器,但不适用于我的手机。我已经在这个网站上使用了很多例子来实现这个目标,但是它根本不工作。它保持关闭我的手机上的应用程序的力量。任何帮助表示赞赏。Android只读文件系统错误
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);
口口声声说空指针。
原来,空指针实际上是一个read_only错误。所以它没有实际写入文件,我得到一个ioexception只读文件系统。它为什么这样做?
答
好的,这里是我最终做的,以获得这个分享照片,以防万一别人需要它。事实证明,我的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;
}
});
}
在尝试写入之前,您需要查看目录的权限。您必须了解UNIX的基本文件系统权限才能执行此分析。 – JoxTraex 2012-03-21 05:49:41
你在清单文件中使用写入权限? – 2012-03-21 05:54:07
是的,我在清单中有write.external.storage – Pestilence 2012-03-21 14:30:59