Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)


問題描述

Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)

I am passing a local file through Javascript to set an img source and I cannot get it working. The javascript to set the path is

<img src='" + the_payload.image + "' width='" + (cellWidth()-20) + "' />

The String that I am sending is the complete file path:

file:///mnt/sdcard/Android/data/com.newvisioninteractive.android.myapp/files/c87eba4a-5349-4a55-baec-cc573a5f7571-thumb.png

I saw a post about ContentProviders and ParcelFileDescriptors but could not get that to work either. 

This is for a Calendar View that displays images on certain days. It works fine when I pass in a static image, just not a local one.

Also, I have set

    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setJavaScriptEnabled(true);

Do I need to set any other Permissions?


參考解法

方法 1:

The solution is in extending the ContentProvider class and Overriding the openFile(Uri, String) method

package com.packagename.provider;
public class MyProvider extends ContentProvider { 
     @Override
     public ParcelFileDescriptor openFile(Uri uri, String mode){
        URI fileURI = URI.create( "file://" + uri.getPath() );
        File file = new File( fileURI );

        ParcelFileDescriptor parcel = null;
        try {
            parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        } catch (FileNotFoundException e) {
            Log.e( TAG, "Error finding: " + fileURI + "\n" + e.toString() );
        }

        return parcel;
     }
}

Then in your AndroidManifest.xml file include 

 <provider
        android:name=".provider.MyProvider"
        android:authorities="com.packagename" />

You can then access your files which would normally be 

 file://sdcard/Android/data/com.packagename/image.jpg

by using 

 content://com.packagename/sdcard/Android/data/com.packagename/image.jpg

So essentially replace file:// with content://com.packagename

方法 2:

try using file:///android_assets/image.png and place images in the android assets

(by SPillaiSPillaiSquiresSquire)

參考文件

  1. Android - Set a Local Image to an img element in WebView (CC BY-SA 3.0/4.0)

#android-webview #Android #android-contentprovider






相關問題

如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)

Phonegap 應用程序不滾動 (Phonegap Application Not Scrolling)

Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)

使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)

WebView LoadURL dan GetView (WebView LoadURL and GetView)

Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)

Android: Chế độ xem hình ảnh chụm-thu phóng với Chế độ xem con được nối thêm (Android: Pinch-zooming ImageView with appended child Views)

android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)

使用 android studio 在 webview 中顯示谷歌表單 (display google forms in webview using android studio)

如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)

android webview 顯示其字體的默認字體系列是什麼? (What is the default font family taken by android webview to show its fonts?)

Android Studio - 視頻音量低 (Android Studio - Video Volume Low)







留言討論