3

I am using camera in my app.I need to take picture and should display it in imageview. I am using the following code to take picture from the camera and display it.

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
    startActivityForResult(intent, 0);
    imageView.setImageURI(capturedImageUri);

This works only for two or sometimes three images,then the imageview doesn't show the image but the image is correctly stored in SD card. Alternatively i have also used

   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
   Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
   Imageview.setImageBitmap(bitmap);

But i am facing the same problem. can any one help me please.

Coffee_lover
  • 545
  • 6
  • 15
Mathan
  • 49
  • 3
  • 10
  • are there any errors showing up in logcat ? – JoxTraex Jul 27 '13 at 04:18
  • Do you get any errors? It looks like you are trying to display the full-sized image taken by camera, which would consume an inordinate amount of memory. – Kai Jul 27 '13 at 04:19
  • No. it does not display any error. i was checked the file size too. its nearly same to the displaying image. and i tried compress too. still i am facing the same problem. – Mathan Jul 27 '13 at 04:20

2 Answers2

1
capturedImageUri  will return path to captured Image not the actual Image..

Also, Important Note-- If you dont need a full sized image use-

   // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);   Comment this line
    Bitmap image = (Bitmap) data.getExtras().get("data");

To get the full sized Bitmap Use following code-

  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                try
                {
                    // place where to store camera taken picture
                    tempPhoto = createTemporaryFile("picture", ".png");
                    tempPhoto.delete();
                }
                catch(Exception e)
                {

                    return ;
                }
                mImageUri = Uri.fromFile(tempPhoto);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
                startActivityForResult(cameraIntent, 1);

private File createTemporaryFile(String part, String ext) throws Exception
    {
           // File to store image temperarily.
        File tempDir= Environment.getExternalStorageDirectory();
        tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
        if(!tempDir.exists())
        {
            tempDir.mkdir();
        }
        return File.createTempFile(part, ext, tempDir);
    }




  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

            ContentResolver cr = getApplicationContext().getContentResolver();
            try {
                cr.notifyChange(mImageUri, null);
                File imageFile = new File(tempPhoto.getAbsolutePath());


            } catch (Exception e) {
                e.printStackTrace();
            }

            Bitmap photo=null;
            if (resultCode == 1) {
                try {
                    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto));
imageView.setImageBitmap(photo);
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                } catch (Exception e) {

                    e.printStackTrace();


}
Mr.India
  • 674
  • 2
  • 9
  • 19
  • Hi Amol Thanks for your response. but still i am facing the same problem. – Mathan Jul 27 '13 at 05:54
  • 1
    @Mathan Do you need full sized Image? or you ok with the thumbnail? Because adding `intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);` will return Full sized image. – Mr.India Jul 27 '13 at 06:03
0

Have you tried putting your setImageUri logic in the activity callback onActivityResult

I think you are trying to supply the image to the imageview before any content is saved to that uri. You need to hook into the callback that gets fired when the camera actually take the picture (i.e. the camera activity finishes and reports its result)

This answer has a full write up

https://stackoverflow.com/a/5991757/418505

possibly something like

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
}
Community
  • 1
  • 1
Selecsosi
  • 1,636
  • 19
  • 23
  • Hi Selecsosi, Thanks for your response. I have used Your suggestion. still the problem is there. – Mathan Jul 27 '13 at 05:56