Search code examples
androidimageviewfilepathandroid-glide

Can not load image from file path to ImageView with Glide


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.


Solution

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