我正在尝试学习如何在我的 android 应用程序中使用相机。我目前有一个带有叠加按钮的工作预览。我已将其设置为成功拍照,但是当我尝试将照片保存到 SD 卡时,它无法正常工作。它总是返回“保存期间出错”。
我知道这不是我的 SD 卡,并且我之前在另一个应用程序中使用过几乎相同的代码来保存位图,它可以成功运行。
我也知道它正在成功拍照,因为我通过让它将照片放入活动中的 ImageView 来测试它。
我不确定为什么这不起作用。
Button takePhoto = (Button)findViewById(R.id.takephoto);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mCamera.takePicture(shutterCallback, rawCallback,
jpegCallback);
}
});
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] arg0, Camera camera) {
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] arg0, Camera camera) {
Bitmap photo = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
tempSavePhoto(photo);
switchToPolaroidView();
}
};
public void tempSavePhoto(Bitmap photo){
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Photos");
File image = new File(root, "toSend.jpg");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
boolean success = false;
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
photo.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Saved successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_SHORT).show();
}
}