2011-05-16 109 views
0

我搜索並嘗試了幾種不同的方法。我在這兩個方面都有所欠缺。這是我目前的方法:從網址加載圖像

package com.dop.mobilevforum; 

import java.io.InputStream; 
import java.net.URL; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.Configuration; 
import android.graphics.drawable.Drawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ImageView; 
import android.widget.VideoView; 

public class Vforum extends Activity 
{ 
    private String imgPath = "http://mysite.com/mv/vfdemo1/slides/slide1.jpg"; 

    private ImageView slideHolder; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.vforum); 

     slideHolder = (ImageView) findViewById(R.id.slideHolder); 

     Drawable drawable = LoadImageFromWebOperations(imgPath); 
     slideHolder.setImageDrawable(drawable); 
    } 

    private Drawable LoadImageFromWebOperations(String url) 
    { 
     try 
     { 
      InputStream is = (InputStream) new URL(url).getContent(); 
      Drawable d = Drawable.createFromStream(is, "src name"); 
      return d; 
     } 
     catch (Exception e) 
     { 
      Log.w("LoadImageFromWebOperations",e.toString()); 
      return null; 
     } 
    } 
} 

的LoadImageFromWebOperations方法返回一個位圖,所以我知道那部分工作。這是我認爲失敗的slideHolder.setImageDrawable(drawable);。這是我的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/mobile_vforum_bg"> 
    <ImageView 
     android:id="@+id/slideHolder" 
     android:layout_width="320px" 
     android:layout_height="240px"> 
    </ImageView> 
</FrameLayout> 

什麼想法?我只是得到一個黑屏,沒有錯誤。

+0

一對夫婦OS項目,我已經看到了剛剛使用的WebView來顯示圖像。 – NickAldwin 2011-05-16 20:38:21

+0

使用網絡視圖是如此簡單! – Ronnie 2011-05-16 22:07:36

回答

2

最好的辦法是使用WebView,但如果你真的想這樣做,不要使用setImageDrawable。你應該使用setImageBitmap。如果你的LoadImageFromWebOperations返回一個位圖,那麼你不應該改變任何東西,但你正在使用的功能,它應該工作順利。

+0

是的,WebView是要走的路。那太容易了。另外,當我說位圖時,我的意思是像一個圖像。我應該更清楚。它確實正在返回Drawable。 – Ronnie 2011-05-16 22:08:28

0

你的代碼工作,我親愛的朋友你的網址不工作http://mysite.com/mv/vfdemo1/slides/slide1.jpg

你可以使用這個它的工作原理100%,而且你的代碼也可以

URL url = new URL("http://variable3.com/files/images/email-sig.jpg"); 
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
slideHolder.setImageBitmap(bmp); 
+0

我的網址確實有效。我改變了它,因爲它是我不希望人們看到的信息。無論如何,我試過你的代碼,我無法使它工作。我結束了使用WebView – Ronnie 2011-05-16 22:09:34