3

I have an Android app with an ImageButton. When user clicks on it, intent launches to show camera activity. When user capture the image, I'd like to save it in drawable folder of the app and display it in the same ImageButton clicked by the user, replacing the previous drawable image. I used the activity posted here: Capture Image from Camera and Display in Activity

...but when I capture an image, activity doesn't return to activity which contains ImageButton.

Edit code is:

public void manage_shop()
{
    static final int CAMERA_REQUEST = 1888;

    [...]
    ImageView photo = (ImageView)findViewById(R.id.getimg);
    photo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(camera, CAMERA_REQUEST); 
        }
    }); 
   [...]
}

And onActivityResult():

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
  ImageButton getimage = (ImageButton)findViewById(R.id.getimg);

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
    {  
        Bitmap getphoto = (Bitmap) data.getExtras().get("data"); 
        getimage.setImageBitmap(getphoto);
    }
}

How can I also store the captured image in drawable folder?

Community
  • 1
  • 1
Andres7X
  • 165
  • 2
  • 4
  • 12

2 Answers2

2

once you've saved the image to a file you can use the following snippet to add to gallery.

ContentValues values = new ContentValues(); 
values.put(MediaStore.Images.Media.TITLE, new File(path).toString());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, path);
getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values);

to save the file to a directory do the following

private saveFileToDir() {
    final InputStream in = Wherever you input stream comes from;

    File f = generatePhotoFile();

    OutputStream out = new FileOutputStream(f);
    byte[] buffer = new byte[1024];

    int len;
    while ((len=in.read(buffer))>0)
    {
        out.write(buffer,0,len);
    }

    in.close();
    out.flush();
    out.close();        
}

private File generatePhotoFile() throws IOException {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat("yyyymmdd_hhmmss");

    String newPicFile = "IMG_"+ df.format(date) + ".jpg";

    File f = new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera/", newPicFile);

    if (!f.exists())
    {
        if(!f.getParentFile().exists())
            f.getParentFile().mkdir();

        f.createNewFile();
    }

    return f;
}       
chris-tulip
  • 1,840
  • 1
  • 15
  • 22
0

How can I also store the captured image in drawable folder?

It's not possible to save an image to the drawable folder dynamically:

See:

android image save to res/drawable folder

and

Write to /res/drawable/ on the fly?

Community
  • 1
  • 1
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71