我有以下代码拍照并保存。但我想将最终图像保存到 SD 卡。
public class TakePhoto extends Activity {
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
iv = (ImageView) findViewById(R.id.imageView1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bm = (Bitmap) data.getExtras().get("data");
writeBitmapToMemory("image.png", bm);
iv.setImageBitmap(bm);
}
public void writeBitmapToMemory(String filename, Bitmap bitmap) {
FileOutputStream fos;
try {
fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
有一点似乎正在发挥作用。我正在尝试保存到 SD 卡,但出现错误:(pastebin.com/FHihS4Wv)
我改变writeBitmapToMemory("/mnt/extSdCard/image.png", bm);
了我的 onActivityResult - 但它因上面的 pastebinned 错误而崩溃