I am trying to learn how to use the camera in my android application. I currently have a working preview with overlaying buttons. I have it set up to successfully take a photo, however when I try to save the photo to the SD card, it is not working correctly. It always comes back "error during saving".
I know that it is not my SD card, and I have used almost the same exact code before in another app to save a bitmap, which works successfully.
I also know that it is successfully taking a photo, because I tested it by having it place the photo into an ImageView in the activity.
I'm not sure why this isn't working.
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();
}
}