0

I am picking image from gallery and showing image on ImageView. I am able to do it successfully. My problem is when i try to set an image that are captured from camera(.jpg images) they are not shown in imageview just a blank screen comes up. Even images of type .jpg are successfully showed that are not captured from camera. Can't understand the reason, may be due to larger size. Help please Following is my code:

public class ImagePick extends Activity {

private Bitmap bitmap;
private ImageView imv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_upload);
imv = (ImageView) findViewById(R.id.targetimage);

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/*");
  startActivityForResult(intent, 0);

}


protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

     switch(requestCode) {
     case 0:
         if(resultCode == RESULT_OK){ 
             Uri selectedImage = imageReturnedIntent.getData();
             String[] filePathColumn = {MediaStore.Images.Media.DATA};

             Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
             cursor.moveToFirst();

             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
             String filePath = cursor.getString(columnIndex); // file path of selected image
             cursor.close();
                   //  Convert file path into bitmap image using below line.
             Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

                   // put  bitmapimage in your imageview
             imv.setImageBitmap(yourSelectedImage);
         }
     }
}
}

Thanks in Advance

nadeem gc
  • 484
  • 1
  • 8
  • 22

2 Answers2

0

you need to use FileInputStream and BufferedInputStream to read the image then you convert the image into bitmap.

    private FileInputStream is = null;
    private BufferedInputStream bis = null;


is = new FileInputStream(new File(imagePath));
        bis = new BufferedInputStream(is);

also check the link

Thanks

Community
  • 1
  • 1
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
0

Instead of:

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex); // file path of selected image

Try:

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);

And close the cursor After the BitmapDecoding.

Remember that the access to the SD card should be done in AsynckTasks. And you can resize the image to fit the screen, that way you won't use a lot of memory, take a look at my other answer Bitmap Out Of Memory Issues. on how to do this.

Community
  • 1
  • 1
nsemeniuk
  • 1,171
  • 1
  • 13
  • 24