0

I have the following code taking a picture and saving it. But I want to save the final image to SD card instead.

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();


        }

    }

Theres one bit that seems to be playing up. I'm trying to save to SD card but it falls over with an error: (pastebin.com/FHihS4Wv)

I changed writeBitmapToMemory("/mnt/extSdCard/image.png", bm);on my onActivityResult – But it falls over with the pastebinned error above

il_guru
  • 8,383
  • 2
  • 42
  • 51
MissCoder87
  • 2,669
  • 10
  • 47
  • 82

1 Answers1

0

According to this question java.lang.IllegalArgumentException: contains a path separator You're getting it because you're trying to access a sub-directory using aString object rather than a File object

Try to pass to the function a File object to that specific image file, instead of a string containing the file name.

Something like this:

@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();

    } 
....

Edit: It should work now, but I didn't test it.

Community
  • 1
  • 1
La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • Thanks for this. One question though, i'm getting an error on the openFileOuput(...) now, the openFileOutput (as seen http://www.tiag.me/pics/error.png) – MissCoder87 Sep 04 '12 at 22:47
  • Thanks for the edit and help. I appreciate it. It doesn't error, but for some reason theres no image. I have my file set as 'File imageFile = new File("/mnt/extSdCard/image.png");' – MissCoder87 Sep 04 '12 at 23:00
  • Did you set the necessary permissions in the manifest? Also, it is possible that it's something with the dirs heirarachy. check this answer for example on how to do this http://stackoverflow.com/a/7844792/975959 – La bla bla Sep 04 '12 at 23:03
  • Yeah I have, it was my own fault a copy and paste error. Thanks a LOT for your help mate, really appreciate it! - Tom – MissCoder87 Sep 04 '12 at 23:14
  • I have one more question if you don't mind (i don't mind asking it as a new question if you do :)) - When it saves the pictures they're really low quality and tiny – MissCoder87 Sep 04 '12 at 23:31
  • I'm not that familiar with it, but the documentation says that png ignores the quality indicator (the 100 in the compress method) try changing it to Bitmap.CompressFormat.JPEG and play with the number (range is 0-100) – La bla bla Sep 05 '12 at 05:32