How can you obtain the path URI
of a resource Drawable
internal%, saved in res
or assets
?
How can you obtain the path URI
of a resource Drawable
internal%, saved in res
or assets
?
/assets
or /raw
, (obviously for /raw
change the folder):
String archivo = "android.resource://"+ getPackageName() + "/assets/mi_recurso";
to get the Uri would be with Uri.parse()
:
Uri ruta = Uri.parse(archivo);
/res
folder is similar: String archivo = "android.resource://"+ getPackageName() + "/drawable/mi_recurso";
Uri ruta = Uri.parse(archivo);
even by means of the resource id:
String archivo = "android.resource://"+ getPackageName() + "/" + R.drawable.mi_recurso;
Uri ruta = Uri.parse(archivo);
getPackageName () is a method to get the name of the package from your application, but you could also write the name directly, for example:
String archivo = "android.resource://com.miaplication.webserveis/drawable/mi_recurso";
Uri ruta = Uri.parse(archivo);
In a SO response to @ ceph3us there is a function that automatically detects where the resource is located.
public static final Uri getUriToResource(@NonNull Context context, @AnyRes int resId) throws Resources.NotFoundException {
Resources res = context.getResources();
Uri resUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + res.getResourcePackageName(resId)
+ '/' + res.getResourceTypeName(resId)
+ '/' + res.getResourceEntryName(resId));
return resUri;
}
Your use
Uri origen = getUriToResource(this,R.drawable.mi_recurso);