2012-07-30 51 views
1

我試圖在web視圖中顯示一個圖像,但我的應用程序一旦到達加載webview的活動就會崩潰。在webview中顯示圖像(不工作)android

WebView myWebView = (WebView) findViewById(R.id.webView1); 
    myWebView.getSettings().setJavaScriptEnabled(true); 
// myWebView.getSettings().setBuiltInZoomControls(true); 
// myWebView.getSettings().setSupportZoom(true); 
    myWebView.loadUrl("http://www.de-saksen.nl/2/images/comprofiler/62_4dc3051f2b262.jpg"); 
//  myWebView.loadData(
//   "<img src='http://www.de-saksen.nl/2/images/comprofiler/62_4dc3051f2b262.jpg'>", 
//   "text/html", "UTF-8"); 

我一直在對代碼進行一些迷惑,但沒有任何幫助。

該活動位於Manifest.xml中,它允許Internet許可。

編輯:閱讀所有awnsers的helpt很多,發現圖像加載,如果我把一個新的活動裏面的代碼,但我需要能夠加載它在這一個:

public class Profileviewer extends ListActivity { 


/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.listplaceholder); 


    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 


    JSONObject json = JSONfunctions.getJSONfromURL("http://de-saksen.nl/deelnemers.txt"); 



    try{ 

     JSONArray deelnemers = json.getJSONArray("deelnemers"); 

     int Key = getIntent().getIntExtra("Key", -1); 

      { HashMap<String, String> map = new HashMap<String, String>(); 
      JSONObject e = deelnemers.getJSONObject(Key); 

      map.put("id", String.valueOf(Key)); 
      map.put("name", "Naam: " + e.getString("naamingw2")); 
      map.put("sex", "Geslacht: " + e.getString("geslacht")); 
      map.put("rank", "Rang: " + e.getString("rang")); 
      map.put("race", "Ras: " + e.getString("ras")); 
      map.put("profession", "Beroep: " + e.getString("beroep")); 
      map.put("skills", "Hobby's: " + e.getString("hobbys")); 
      map.put("lvl", "Level: " + e.getString("level")); 
      map.put("avatar", e.getString("avatar")); 
      mylist.add(map);    
     } 



    }catch(JSONException e)  { 
     Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.profile, 
        new String[] { "name", "sex", "rank", "race", "profession", "skills", "lvl" }, 
        new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2, R.id.item_subtitle3, R.id.item_subtitle4, R.id.item_subtitle5, R.id.item_subtitle6 }); 

    setListAdapter(adapter); 




    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
      Intent intent = new Intent(Profileviewer.this, Listviewer.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 
     } 
    }); 



} 
} 

因爲在這個活動中,我有當前選定的用戶。

+0

我只是想你的代碼。它工作正常。即加載圖像沒有任何問題。在哪個版本的操作系統上,你正在嘗試這個代碼? – sunil 2012-07-30 12:24:46

回答

2

使用本

byte[] imageRaw = null; 
try { 
URL url = new URL("http://some.domain.tld/somePicture.jpg"); 
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
ByteArrayOutputStream out = new ByteArrayOutputStream(); 

int c; 
while ((c = in.read()) != -1) { 
    out.write(c); 
} 
out.flush(); 

imageRaw = out.toByteArray(); 

urlConnection.disconnect(); 
in.close(); 
out.close(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

    String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT); 

    String urlStr = "http://example.com/my.jpg"; 
    String mimeType = "text/html"; 
    String encoding = null; 
    String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />"; 

    WebView wv; 
    wv = (WebView) findViewById(R.id.webview); 
    wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr); 
0

我剛剛在模擬器上運行該代碼,它的工作原理很好。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <WebView 
     android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

</LinearLayout> 

Android 3.2的 沒有必要啓動AsyncTasks。

0
public class WebviewActivity extends Activity {  
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   

    WebView wv = (WebView) findViewById(R.id.webview1); 
    WebSettings webSettings = wv.getSettings(); 
    webSettings.setBuiltInZoomControls(true); 
    wv.loadUrl("http://ecx.images-amazon.com/images/I/41HGB-W2Z8L._SL500_AA300_.jpg"); 

    }  

} 
0

檢查了這一點,你將能夠顯示在網頁視圖圖像。

[http://stackoverflow.com/questions/5267124/how-to-display-image-with-webview-loaddata][1] 
1
public class WebviewActivity extends Activity {   
      public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);   
      setContentView(R.layout.main);   

    WebView wv = (WebView) findViewById(R.id.webview1);   
      WebSettings webSettings = wv.getSettings();   
      webSettings.setBuiltInZoomControls(true);   
      wv.loadUrl("http://ecx.images-amazon.com/images/I/41HGB-W2Z8L._SL500_AA300_.jpg"); 

    }  

} 

XML

<?xml version="1.0" encoding="utf-8" ?> 
    <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
    <WebView 
    android:id="@+id/webview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout>