0

I already tried the following but without success. My project is using API level 4.

from stackoverflow

from android documentation

Here is my code:

webView.loadUrl("android.resource://com.package.name.from.manifest/" + R.drawable.pic); 

And I've got the following error in the WebView widget:

Web page not available

The Web page at android.resource://com.package.name.from.manifest/2130837504 might be temporarily down or it may have moved permanently to a new web address.

Am I missing something? Should I create html page which will include a link to the resource as < img ...> tag?

Community
  • 1
  • 1
Genry
  • 1,358
  • 2
  • 23
  • 39

4 Answers4

4

You can get image path like this.

file:///android_res/drawable/YOUR_FILE_NAME

Pass this path to tag in html.It will work.

vntstudy
  • 2,038
  • 20
  • 24
2

I believe you should have your drawables in assets/ folder, then refer to them through a URI in the form of file:///android_asset/drawable_name.png, for example.

tsadvanced
  • 66
  • 4
  • This will probably work, but I don't want to retrieve file, since files can be different extension, but same name. For example: in several times the file can be 01.png, on other can be 01.jpg – Genry Feb 21 '12 at 18:12
  • 1
    Which means, that I need a link which will directly retrieve resource by resource id and not the file name – Genry Feb 21 '12 at 18:30
2

In the end I have done the following:

I had an array of file name extensions of each of the image in resources. And then I could call the following method with the resource ID and it's file name extension.

protected String getResourceURL(int resourceId, String extension) {
    String resName = getResources().getResourceName(resourceId);
    int index = resName.indexOf("/");
    resName = resName.substring(0, index);
    index = resName.indexOf(":");
    String resType = resName.substring(index + 1);

    StringBuilder link = new StringBuilder(128);
    link.append("file:///android_res/");
    link.append(resType).append("/");
    link.append(getResources().getResourceEntryName(resourceId));
    link.append(".").append(extension);
    return link.toString();
}
Genry
  • 1,358
  • 2
  • 23
  • 39
-3

I hope it is helpfull.. For getting image from res/drawable..

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
  • I think you didn't understand my question. WebView.loadUrl(String url). How can I receive link from Drawable? – Genry Feb 21 '12 at 18:08