写真を撮ってファイルに保存しようとしています。問題は、ビットマップをファイルに保存しようとしていることです。これが私のコードです:
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)
以下のコードは、問題の解決策の1つです。
static Uri capturedImageUri = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView) this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(), (cal.getTimeInMillis() + ".jpg"));
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
capturedImageUri = Uri.fromFile(file);
Intent i = new Intent(Android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(i, CAMERA_RESULT);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
//Bitmap photo = (Bitmap) data.getExtras().get("data");
//imageView.setImageBitmap(photo);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), capturedImageUri);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
あなたのエラーは明らかにJava.io.FileNotFoundException: /filename
正確なパス "/ sdcard/filename"を入力してください
new FileOutputStream(getExternalStorageDirectory()+"filename");
OR
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/name.png";
注:権限WRITE_EXTERNAL_STORAGEをマニフェストファイルに追加します。