1

I have an ImageView into which I want to be able to load an image from:

  1. gallery - works just fine

  2. camera - does not load anything

The logs while using these two different actions are very similar besides the camera action displays to the end this:

  • 10-21 23:24:18.304: D/android.widget.GridLayout(4447): horizontal constraints: x6 - x0 > 720, x5 - x0 > 720, x5 - x4 < 20, x4 - x3 < 142, x3 - x2 < 70, x2 - x1 < 32, x1 - x0 < 138 are inconsistent; permanently removing: x5 - x4 < 20.
  • 10-21 23:24:18.324: D/android.widget.GridLayout(4447): vertical constraints: y1 - y0 > 468, y2 - y1 > 30, y3 - y2 > 120, y3 - y0 > 1140, y4 - y3 > 612, y4 - y0 < 1140, y3 - y2 < 120, y2 - y1 < 30 are inconsistent; permanently removing: y4 - y0 < 1140, y3 - y2 < 120.

I have a feeling this is an explanation why the image has not been displayed. The app does not segfault or anything, nothing happens from the UI perspective.

The code (shared by both, the camera action and the gallery action) is:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode == Activity.RESULT_OK) {
    Uri uri = data.getData();

    if ((requestCode == SELECT_PICTURE) || (requestCode == PICTURE_RESULT)) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getRealPathFromURI(selectedImageUri);
        mImageView = (ImageView) findViewById(R.id.imageView1);
        int x = mImageView.getWidth();
        int y = mImageView.getHeight();
        if (x == 0 || y == 0) {
            Display d = getWindowManager().getDefaultDisplay();
            x = d.getWidth();
            y = d.getHeight();
        }
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options(); 
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(selectedImagePath, opts);
            // Calculate inSampleSize
            opts.inSampleSize = calculateInSampleSize(opts, x, y);                  
            // Decode bitmap with inSampleSize set
            opts.inJustDecodeBounds = false;
            mPicture = BitmapFactory.decodeFile(selectedImagePath, opts);
            mPicture.recycle(); //otherwise multiple calls segfault
            // create a matrix object
            Matrix matrix = new Matrix();
            matrix.postRotate(90); // clockwise by 90 degrees
            // create a new bitmap from the original using the matrix to transform the result
            Bitmap rotatedBitmap = Bitmap.createBitmap(mPicture, 0, 0, mPicture.getWidth(), mPicture.getHeight(), matrix, true);

            //set image view
            mImageView.setImageBitmap(rotatedBitmap);
        } catch (Exception e) {                 
            System.out.println("Bitmap could not be decoded." + e.getMessage());
        }
    }

The path is correct, the Bitmap is not null, everything looks just OK but the image is not displayed. Thanks for a help!

Jana
  • 5,516
  • 5
  • 23
  • 29
  • 1
    Putting `mPicture.recycle();` while you are still using `mPicture` is a recipe for disaster - the online help says `it should only be called if you are sure there are no further uses for the bitmap`, which is obviously untrue in your example. I've had success [following the instructions here](http://stackoverflow.com/questions/2641726/decoding-bitmaps-in-android-with-the-right-size). – Ken Y-N Oct 22 '12 at 07:31
  • @KenY-N thanks! Before, it was giving me OOM without using recycle. However, it changed after sending the Uri to the camera intent as NARESH suggested. This also fixed the loading of the picture from the camera. – Jana Oct 23 '12 at 04:10

2 Answers2

1

I think the error is in file creation

This below code worked for me

File photofile = new File(Environment
                    .getExternalStorageDirectory(),"sample"
                    + System.currentTimeMillis() + ".jpg");
            photFileUri = Uri.fromFile(photofile);
            photoPath = photofile.getAbsolutePath();
            Log.v("camera photo path is    ","          "+photoPath);
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photFileUri);
            cameraIntent.putExtra("return-data", true);
            startActivityForResult(cameraIntent,1);

In OnActivityForResult() method you will get the path

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

    switch (requestCode) {
    case 1:
                //here u can use **photoPath** to display image
}

}

BenMorel
  • 34,448
  • 50
  • 182
  • 322
NARESH REDDY
  • 682
  • 4
  • 11
0

Try this :--

 Handler handler = new Handler();
 handler.posDelayed(new Runnable){
  @Override
public void run() {
 //Your ImageView Code & setImageBitmap
}

  }, 20);
Deepanker Chaudhary
  • 1,694
  • 3
  • 15
  • 35