0

I'm trying to get image from gallery and store the path to the database for further use. File path is saved in database but I am unable to get the view on ImageView.

File path is: /storage/emulated/0/DCIM/Camera/image.jpg

Here I'm getting the image path as imgS

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        img.setImageURI(imageUri);
        imgS = getPath(getApplicationContext(), imageUri);
    }
}

public static String getPath(Context context, Uri uri) {
    String result = null;
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndex("_data");
            result = cursor.getString(column_index);
        }
        cursor.close();
    }
    if (result == null) {
        result = "Not found";
    }
    return result;
}

Here I'm trying to retrieve the path and show the image in image view.

 File f = new File(item.getImg());
    Glide.with(c).load(f).into(holder.img);
    holder.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(c, f.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            if (f.exists()) {
                Toast.makeText(c, f.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            }
        }
    });

Note: File exist at the path.

Zeeshan Ali
  • 667
  • 8
  • 16
  • What OS version are you using? Accessing files directly by path generally [won't work in Android 11](https://developer.android.com/about/versions/11/privacy/storage). You'll want to use MediaStore, as per the ["Media" row of this table](https://developer.android.com/training/data-storage). – Jake Lee May 05 '21 at 17:33
  • I'm using android 10. – Zeeshan Ali May 05 '21 at 17:38
  • Dont try to get a file path. You can use the uri directly to handle the file. That file path is not accessable on an Android 10 device. The uri is. – blackapps May 05 '21 at 19:01
  • Store data.getData().toString() for future use. For an ImageView.setImageUri() for example. – blackapps May 05 '21 at 19:03
  • `int column_index = cursor.getColumnIndex("_data");` You better check for value -1. – blackapps May 05 '21 at 19:05

1 Answers1

1

Just need to add android:requestLegacyExternalStorage="true" to Application tag in Manifest.xml .

Zeeshan Ali
  • 667
  • 8
  • 16