Use Glide to load an image that is stored in internal storage

1

I'm trying to load an image from the internal memory using the Glide library but it seems impossible.

imagePath="/storage/emulated/0/Pictures/IMG-20161029-WA0025.jpg"

This is the simplest code but it does not work.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView = (ImageView)findViewById(R.id.foto);
     File imgFile = new  File("/storage/emulated/0/Pictures/IMG-20161029-WA0025.jpg");
    Glide.with(this).load(imgFile).into(imageView);

}
    
asked by Melquiades Rodriguez Pinto 15.05.2017 в 06:08
source

2 answers

0

If you have the correct path you can pass a URI instance to Glide:

File file = new File(completePath);

Uri imageUri = Uri.fromFile(file);
Glide.with(this).load(imageUri).into(imageView);
    
answered by 15.05.2017 в 09:22
0

You must get the Uri from the path, ensuring that the resource exists:

File imgFile = new  File("/storage/emulated/0/Pictures/IMG-20161029-WA0025.jpg");
Uri imageUri = Uri.fromFile(imgFile);

Glide.with(this)
            .load(imageUri)
                    .into(imageView);

It is important to note that in versions after Android N the method Uri.parse() should be used instead of Uri.fromFile() , therefore the correct thing is to validate:

String filePath = "/storage/emulated/0/Pictures/IMG-20161029-WA0025.jpg";
File imgFile = new  File(filePath);
Uri imageUri = null;

    if (Build.VERSION.SDK_INT >=  Build.VERSION_CODES.N) {
          imageUri = Uri.parse(filepath);        
    } else{              
          imageUri = Uri.fromFile(imgFile);
    }

//Carga imagen.
Glide.with(this)
            .load(imageUri)
                    .into(imageView);
    
answered by 12.12.2017 в 00:19