0

I am using the following code to take picture, using the device camera. I am new to android. Can anybody please help me and tell me where I should specify the path. I want to save images in a separate folder in sd card. Any help is deeply appreciated.

private static final int CAMERA_PIC_REQUEST = 2500;

bcontinue.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
          Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
     }
 });

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == CAMERA_PIC_REQUEST && resultCode==RESULT_OK) 
        {

            try{
                 Byte image1 = (Byte) data.getExtras().get("data");
                     FileOutputStream fos = openFileOutput("filename.bmp", Context.MODE_PRIVATE);
                 fos.write(image1);
                 fos.close();
                    }
                    catch(Exception e){

                    }           
              Bitmap image = (Bitmap) data.getExtras().get("data");
              ImageView imageview = (ImageView) findViewById(R.id.imageView1);               
              imageview.setImageBitmap(image);

              Context context = getApplicationContext();
                CharSequence text = "Click on the image!";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
        }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242

1 Answers1

0

The below code will start the default camera and have the camera save the image to the specified uri. The key is to put the extra "MediaStore.EXTRA_OUTPUT" along with the desired uri.

File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + image_name + ".jpg");
Uri imageUri = Uri.fromFile(file);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, 0);
Rahul Gupta-Iwasaki
  • 1,683
  • 2
  • 21
  • 39
  • Thanks buddy, will check it and come back with the comments soon – kiran francis Oct 24 '11 at 04:55
  • Bro I have some more doubt.I want to save pics in a folder called "All" in my sd card. How should I set that path now? – kiran francis Oct 24 '11 at 05:03
  • sry it took me so long to get back.. erm, if I remember correctly if you just change the path you're opening to /All/... you should be fine.. for example: `File file = new File(Environment.getExternalStorageDirectory().getPath() + "/All/" + image_name + .jpg);` – Rahul Gupta-Iwasaki Oct 24 '11 at 06:00
  • thanks bro, got it right. Now the image is getting saved. But I am getting a force close now after I save the image. Should I make any changes to my previous code, i mean in the onActivityResult part? – kiran francis Oct 24 '11 at 06:06
  • It seems like according to [http://developer.android.com/reference/android/provider/MediaStore.html] that if you pass a URI to store the photo in, a bitmap will not be returned as an extra. In other words, when the way you're trying to set the imageview image by getting the image from the extra will not work. Rather, you will have to go to the uri that you saved the image from and get it from there. – Rahul Gupta-Iwasaki Oct 24 '11 at 06:26
  • if the image's uri is mImageUri, then you could use this code to set mThumbnail to your image: `mThumbnail.setImageBitmap(ActivityReviewImage.decodeFile(this, mImageUri, 150)); ` – Rahul Gupta-Iwasaki Oct 24 '11 at 06:28
  • Thanks again bro,can u give me a sample code to retrieve the images from this directory using another activity? – kiran francis Oct 24 '11 at 06:29
  • [http://stackoverflow.com/questions/4181774/show-image-view-from-file-path-in-android] has some sample code for how to get a Bitmap/set an image view from a file. – Rahul Gupta-Iwasaki Oct 24 '11 at 06:39