1

I've problem with packing images for my application. I've raw folder in my project. The folder contains images in jpg, like img1.jpg, img2.jpg. I need to view these files in WebView in my application.

I try

String Url="file:///android_res/raw/"+neccessary_file";
WebView.LoadUrl(Url);

When i try application webview send 404 Page, file not found. What can i do for correct?

Legich
  • 27
  • 6

3 Answers3

0

Try to append your URL with

String html = "<html><head></head><body><img src=\""+ URL + "\"></body></html>";
webView.loadDataWithBaseURL("", html, "text/html","utf-8", ""); 

Also Refer this link https://stackoverflow.com/a/14405801/1208563

Community
  • 1
  • 1
Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
0

Using the resource id, the format is:

"android.resource://[package]/[res type]/[res name]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myfile");
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
  • I need to use name, because i select file name from database. How i can get resources in this case. I can get id of resource with id = getResources().getIdentifier(neccessary_image_name, "raw", "com.android.project"); But how use it in my code? – Legich Nov 28 '13 at 08:07
0

I know it's late but maybe help someone else.

        WebView webView = new WebView(context);
        webView.setWebViewClient(new WebViewClient() {

            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                return interceptRequest(url);
            }

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                try {
                    return interceptRequest(request.getUrl().toString());
                } catch (Exception e) {
                    Log.w("WebViewClient.shouldInterceptRequest", "request = " + request, e);
                }
                return null;
            }

            private WebResourceResponse interceptRequest(String url) {
                WebResourceResponse webResourceResponse = null;
                try {
                    Resources resources = context.getResources();
                    if (url.startsWith("file:///android_res/raw/")) {

                        String resName = url.substring("file:///android_res/raw/".length());
                        int resourceId = resources.getIdentifier(resName, "raw", Lambo.getAppContext().getPackageName());

                        TypedValue value = new TypedValue();
                        resources.getValue(resourceId, value, false);
                        String typeValueString = value.string.toString();
                        String extension = typeValueString.substring(typeValueString.lastIndexOf('.') + 1);
                        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                        InputStream inputStream = resources.openRawResource(resourceId);
                        webResourceResponse = new WebResourceResponse(mimeType, null, inputStream);
                    }
                } catch (Exception e) {
                    Log.w("WebViewClient.interceptRequest", "url = " + url, e);
                }
                return webResourceResponse;
            }

        });
MadMurdok
  • 570
  • 6
  • 8