3

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();
    }
}
SillyFidget
  • 197
  • 3
  • 16

2 Answers2

1

Add this to your manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

It is more than likely, that this app does not have permission to save to the SDCard where your other does. Adding this grants permission

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
1

I tried a different approach and it worked. Not sure why the other one wasn't working, but it is saving the photo just fine now. Here is what I used instead:

public void tempSavePhoto(Bitmap photo){

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);


    File f = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "test.jpg");
    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));


}

Please note I got this idea from this post, and voted up the answer that helped me.

Community
  • 1
  • 1
SillyFidget
  • 197
  • 3
  • 16