Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the file path with the prefix file:// in android?

This will give me a file path like this: /storage/emulated/0/myimage.jpg

File imageFile = File(Environment.getExternalStorageDirectory(),   "myimage.jpg");
String path = destination.getAbsolutePath();
Log.d("path", path);

If I use this path to load the image into an ImageView using Picasso, it won't work.

Picasso.with(this.context).load("/storage/emulated/0/myimage.jpg").into(imageView);

It works only if I prefix the path with file://

Picasso.with(this.context).load("file:///storage/emulated/0/myimage.jpg").into(imageView);

Is there a way to get the file path with this file:// instead of getting the absolute path and then prepend this manually?

like image 206
s-hunter Avatar asked Oct 23 '25 08:10

s-hunter


1 Answers

You need to use Uri.fromFile to get URI from file:

Uri uriFileName = Uri.fromFile(yourFile);
String uriStr = uriFileName.toString();

Example: "file:///tmp/android.txt"

like image 150
global_warming Avatar answered Oct 24 '25 22:10

global_warming