3

I am interested in setting an image view to a picture just taken by the camera. The camera application works just fine and it saves the image on the sd card in the appropriate location, but when I try to load the image and set it to the image view, it stays blank. Instead of using the path of the image recently taken, I also tried hard coding the path of an existing image but I get the same issue. I have checked other threads but I can't see any differences in my code.

This is the camera function:

private void takePicture(){
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI"+job_num); 
    image_name = username+"_"+date+".png";
    File image_file = new File(imagesFolder, image_name);
    while(image_file.exists()){
        image_name = username+"-"+date+"("+ image_count+").png";
        image_count+=1;
        image_file = new File(imagesFolder,image_name);
    }
    image_path = imagesFolder+image_name;
    Uri uriSavedImage = Uri.fromFile(image_file);
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    int request_code = 100;
    startActivityForResult(imageIntent, request_code);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){ 
        ImageView thumb = (ImageView) findViewById(R.id.thumbnail);
        Bitmap bmp = BitmapFactory.decodeFile(image_path);
        thumb.setImageBitmap(bmp);
        Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();
    }
    else
        Toast.makeText(this,"Error Saving Image", Toast.LENGTH_SHORT).show();
}

Finally here is the ImageView:

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/camera"
    android:layout_marginTop="10dp"
    android:contentDescription="@string/picture_thumbnail"/>

What needs to be changed? Thanks!

Matt Fritze
  • 325
  • 1
  • 5
  • 17

2 Answers2

6

Try this :

    final ImageView thumb = (ImageView) findViewById(R.id.thumbnail);
    thumb.post(new Runnable() {
        @Override
        public void run()
        {
            Bitmap bmp = BitmapFactory.decodeFile(image_path);
            thumb.setImageBitmap(bmp);
            Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();
        }
    });

But i don't recommend you to decode bitmap on UI thread.

From here :

The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data". The following code retrieves this image and displays it in an ImageView.

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    mImageView.setImageBitmap(mImageBitmap);
}

Note: This thumbnail image from "data" might be good for an icon, but not a lot more. Dealing with a full-sized image takes a bit more work.

Best wishes.

Yakiv Mospan
  • 8,174
  • 3
  • 31
  • 35
  • Thanks for the response. Unfortunately, nothing shows up on my screen but the image still gets saved successfully. – Matt Fritze Jun 21 '13 at 20:31
  • Are you sure that bitmap isn't null ? Provide please your "image_path". – Yakiv Mospan Jun 21 '13 at 20:48
  • Here you can find all that you need to take and save photo via intent - http://developer.android.com/training/camera/photobasics.html – Yakiv Mospan Jun 21 '13 at 20:51
  • image_path is defined in the first function, "takePicture" and was defined incorrectly until you pointed that out, thanks. Even with the correct path though, nothing displays. It is supposed to be image_path = Environment.getExternalStorageDirectory()+"/resources/resources/WI1/"+image_name. Scratch that, it works now. Thank you. – Matt Fritze Jun 21 '13 at 20:56
  • Updated answer, try to get your image like that and look throw Android Developer guide. – Yakiv Mospan Jun 21 '13 at 20:59
  • Would you happen to know how to set the orientation to vertical instead of horizontal? – Matt Fritze Jun 21 '13 at 20:59
  • You can find some notes here - http://stackoverflow.com/questions/12672951/lock-your-camera-view-to-landscape-mode-in-android – Yakiv Mospan Jun 21 '13 at 21:04
  • You can rotate image - http://stackoverflow.com/questions/8722359/scale-rotate-bitmap-using-matrix-in-android – Yakiv Mospan Jun 21 '13 at 21:23
1

Try this code in onActivityResult

  InputStream stream = null;
 if (bitmap != null) {
      bitmap.recycle();
    }
    stream = getContentResolver().openInputStream(data.getData());
    bitmap = BitmapFactory.decodeStream(stream);
    stream.close();
    thumb.setImageBitmap(bitmap);

Its always recommended to recycle the bitmap.SO1 & Google Dev

Edit Try this code

 if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
Community
  • 1
  • 1
MDMalik
  • 3,951
  • 2
  • 25
  • 39