Get the URI of a Drawable in Res or Assets in Android

1

How can you obtain the path URI of a resource Drawable internal%, saved in res or assets ?

    
asked by Webserveis 10.10.2016 в 23:14
source

2 answers

3

To get the route Uri from a resource of /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);

To obtain the Uri path of a resource within the /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);
    
answered by 10.10.2016 / 23:19
source
1

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);
    
answered by 11.10.2016 в 00:26