将照片保存到存储卡而不是本地
问题描述:
我用以下代码拍照并保存。但我想将最终的图像保存到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)
我在我的onActivityResult改变writeBitmapToMemory("/mnt/extSdCard/image.png", bm);
- 但它与pastebinned错误上述
答
根据倒下对这个问题java.lang.IllegalArgumentException: contains a path separator 你得到它,因为你试图使用String
对象,而不是一个File
对象
尝试传递给函数的一个File
对象到特定的图像文件来访问一个子目录,而不是一个包含文件名的字符串。
事情是这样的:
@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");
File imageFile = new File("image.png");
writeBitmapToMemory(imageFile, bm);
iv.setImageBitmap(bm);
}
public void writeBitmapToMemory(Filefile, Bitmap bitmap) {
FileOutputStream fos;
try {
// fos = this.openFileOutput(file, Context.MODE_PRIVATE);
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
....
编辑: 应该现在的工作,但我没有测试它。
感谢您的支持。有一个问题,我现在在openFileOuput(...)上出现错误,openFileOutput(如http://www.tiag.me/pics/error.png) – TMB87
是的,对不起。在几分钟内编辑 –
已编辑。现在检查它 –