1. Check out our companion app, Forums for Android! Download from Google Play

How to display an image from the SDCARD in WebView

Discussion in 'Android Lounge' started by afoualer, Jul 13, 2010.

  1. afoualer

    Thread Starter
    38

    Mar 17, 2010
    107
    18
    38
    When I started to display an image from the SDCARD in WebView, I got errors like:
    " Not allowed to load local resource: file://".
    I
     

    Advertisement

    baroquedub likes this.
  2. baroquedub

    baroquedub New Member
    25

    Jan 5, 2010
    2
    0
    25
    Been struggling with this for days - thank you! Creating a custom ContentProvider did work but this is so much easier. Here's my full working code for anyone interested:

    Code (Text):
    1. package uk.co.baroquedub.downloadfile;
    2.  
    3. import java.io.FileOutputStream;
    4. import java.io.InputStream;
    5. import java.net.HttpURLConnection;
    6. import java.net.URL;
    7.  
    8.  
    9. import android.app.Activity;
    10. import android.os.Bundle;
    11. import android.util.Log;
    12. import android.webkit.WebView;
    13.  
    14.  
    15. public class DownloadToPhone extends Activity {
    16.     private static final String TAG = "DownloadToPhone";
    17.  
    18.     protected static final String IMAGE_FILENAME = "news-thumb.png";
    19.     protected static final String IMAGE_SOURCE = "http://developerlife.com/theblog/wp-content/uploads/2007/11/"+IMAGE_FILENAME;
    20.  
    21.     private static final String PACKAGE_NAME = "uk.co.baroquedub.downloadfile";
    22.     private static final String URI_PREFIX = "file:///data/data/"+PACKAGE_NAME+"/files/";
    23.    
    24.    
    25.     /** Called when the activity is first created. */
    26.     @Override
    27.     public void onCreate(Bundle savedInstanceState) {
    28.         super.onCreate(savedInstanceState);
    29.         setContentView(R.layout.main);
    30.        
    31.         try {
    32.             // download an image from the web... in the background
    33.             {
    34.               Runnable getImage = new Runnable() {
    35.                 public void run() {
    36.  
    37.                   try {
    38.                     Log.i(TAG, "MainDriver: trying to download and save PNG file to user.dir");
    39.                    
    40.                     URL u = new URL(IMAGE_SOURCE);
    41.                     HttpURLConnection c = (HttpURLConnection) u.openConnection();
    42.                     c.setRequestMethod("GET");
    43.                     c.setDoOutput(true);
    44.                     c.connect();
    45.  
    46.                     FileOutputStream f = openFileOutput(IMAGE_FILENAME, Activity.MODE_WORLD_WRITEABLE);
    47.  
    48.                     InputStream in = c.getInputStream();
    49.  
    50.                     byte[] buffer = new byte[1024];
    51.                     int len1 = 0;
    52.                     while ( (len1 = in.read(buffer)) > 0 ) {
    53.                         f.write(buffer,0, len1);
    54.                     }
    55.  
    56.                     f.close();
    57.  
    58.                     Log.i(TAG, "successfully downloaded IMAGE file to user.dir");
    59.                     WebView web = (WebView) findViewById(R.id.webview);
    60.                     initWebkit(web);
    61.  
    62.                   }
    63.                   catch (Exception e) {
    64.                     Log.e(TAG, "could not download and save IMAGE file", e);
    65.                   }
    66.  
    67.                 }
    68.               };
    69.               new Thread(getImage).start();
    70.             }
    71.  
    72.           }
    73.           catch (Exception e) {
    74.             Log.e(TAG, "ui creation problem", e);
    75.           }
    76.  
    77.     }
    78.    
    79.     private static void initWebkit(WebView web) {
    80.    
    81.         String html = new String();
    82.         html = ("<html><body><header><h1>Title</h1></header><section><img src=\""+URI_PREFIX+IMAGE_FILENAME+"\" align=left><p>Content</p></section></body></html>" );
    83.  
    84.         web.loadDataWithBaseURL(URI_PREFIX,
    85.         html,
    86.         "text/html",
    87.         "utf-8",
    88.         "");
    89.  
    90.     }
    91. }
     

Share This Page

Loading...