-1

I'm trying to load an image from gallery on android but when I pick the image, it doesn't display in my ImageView,and I get the error "open failed ENOENT (No Such file or directory) can anyone help me please?

Here's my code :

    private static int RESULT_LOAD_IMAGE = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}


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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }


}
aoulet
  • 3
  • 3
  • Guess the MediaStore.Images.Media.DATA is not correctly set for your device. Maybe try http://stackoverflow.com/questions/5858107/how-to-get-file-path-from-sd-card-in-android ? – mach Jun 12 '13 at 09:53

2 Answers2

1

Try Like this..

/** Called when the activity is first created. */
    ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.imgView);

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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    String picturePath;
    String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(selectedImage , projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                picturePath= cursor.getString(column_index);
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }
    }
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

Same code is working on my side. Check with your media images.

chaitanya
  • 1,726
  • 2
  • 17
  • 13