My application handles writing and reading files, and I'm a bit confused about how and when to use each of these types of Uri.
I think it would be best to understand the difference between these to find out when it would be the correct use of each.
My application handles writing and reading files, and I'm a bit confused about how and when to use each of these types of Uri.
I think it would be best to understand the difference between these to find out when it would be the correct use of each.
File URI and Content URI are Uniform Resource Identifier , identify a particular resource .
Path, usually in the form of a string , identifies a location in a "File System". Indicates the path, through the folder hierarchy, to a file.
Any of these can be used to identify a file on the device. What determines the use of one or the other is the medium (api / class / method) you will use to get it.
Examples:
The class File has a constructor that receives a StringPath and another that receives a URI.
However, the URI must have a scheme equal to file:
, it must be a File Uri.
A Content Uri can identify an image in the gallery and be used to get the path to it .
public String getImagePath(Uri contentUri) {
String[] campos = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, campos, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
Android target applications (API level 24) will not be able to expose File URI's out of the application. If an intent contains a URI of this type, a FileUriExposedException
will be cast.
In this case , a URI of type Content URI should be used and grant a temporary access permission.