可能重复:Android Camera - 将图像保存到SD卡中的新文件夹中
我正在尝试拍照并将其保存到文件中 . 问题出现了我试图将位图保存到文件 . 这是我的代码:
private void takePic() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView test = (ImageView) findViewById(R.id.test);
test.setImageBitmap(photo);
try {
FileOutputStream out = new FileOutputStream("filename");
photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
和logcat中的异常:
04-02 14:46:51.975: W/IInputConnectionWrapper(2225): showStatusIcon on inactive InputConnection
04-02 14:46:56.135: W/System.err(2225): java.io.FileNotFoundException: /filename (Read-only file system)
04-02 14:46:56.135: W/System.err(2225): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
04-02 14:46:56.145: W/System.err(2225): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
04-02 14:46:56.145: W/System.err(2225): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
04-02 14:46:56.145: W/System.err(2225): at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
04-02 14:46:56.145: W/System.err(2225): at java.io.FileOutputStream.<init>(FileOutputStream.java:144)
2 回答
以下代码是您的问题的解决方案之一::
你的错误清楚地说明了
java.io.FileNotFoundException: /filename
请提供确切的路径“/ sdcard / filename”
要么
注意:在清单文件中添加权限WRITE_EXTERNAL_STORAGE .