2012-02-02 90 views
0

從網絡加載圖像時出現黑屏。將Web圖像加載到Android時出現黑色屏幕

我把網頁從網頁上轉到一個畫布,然後將其設置爲ImageView。

現在,當我加載它時,圖像中出現黑屏。

活動代碼:

public class ImageActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView i = (ImageView) findViewById(R.id.imageView1); 

     try { 
      String url = "http://www.rotoworld.com/images/headshots/NBA/1372.jpg"; 
      Drawable image = ImageOperations(this, url); 
      i.setImageDrawable(image); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     i.setMinimumWidth(22); 
     i.setMinimumHeight(22); 

     i.setMaxWidth(22); 
     i.setMaxHeight(22); 

    } 

    private Drawable ImageOperations(Context ctx, String url) { 
     try { 
      InputStream is = (InputStream) this.fetch(url); 
      Drawable d = Drawable.createFromStream(is, "src"); 
      return d; 
     } catch (MalformedURLException e) { 
      return null; 
     } catch (IOException e) { 
      return null; 
     } 
    } 

    public Object fetch(String address) throws MalformedURLException, IOException { 

     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    } 

} 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" /> 

回答

0

通常的原因打開的時候得到一個黑色的屏幕活動中和roid是需要很長時間來創建活動。

我認爲你應該把你的圖像取入AsyncTask或類似的東西。

+0

謝謝!從來沒有聽說過AsyncTask。那裏有好的例子嗎? – user1163234 2012-02-02 09:25:46

+0

@ user1163234文檔通常很不錯。 [AsyncTask]的文檔(http://developer.android.com/reference/android/os/AsyncTask.html)包含了一個關於如何使用它的例子。 – tidbeck 2012-02-02 09:42:09

0

嘗試......

try 
      { 
       URL feedImage = new URL(imageUrl); 
       HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection(); 
       InputStream is = conn.getInputStream(); 
       Bitmap img = BitmapFactory.decodeStream(is); 
       i.setImageBitmap(img); 



      } 
      catch (MalformedURLException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
+0

感謝您的快速響應!沒有工作我收到默認設置的圖標。我將XML添加到我的問題謝謝你的時間! – user1163234 2012-02-02 08:15:13

0

嘗試this..it可能有助於您

public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
ImageView imageView= (ImageView)findViewById(R.id.imageView1); 
      new loadImageTask().execute("http://www.rotoworld.com/images/headshots/NBA/1372.jpg"); 
     } 

     public static Drawable LoadImageFromWeb(String url) 
      { 
       try 
       { 
        InputStream is = (InputStream) new URL(url).getContent(); 
        Drawable d = Drawable.createFromStream(is, ""); 
        return d; 

       } 
       catch (Exception e) 
       { 
        return null; 
       } 


      } 

      public class loadImageTask extends AsyncTask<String, Void, Void> 
      { 

       Drawable imgLoad; 

       @Override 
       protected void onPreExecute() 
       { 
        super.onPreExecute(); 
       } 

       @Override 
       protected Void doInBackground(String... params) 
       { 
        imgLoad = LoadImageFromWeb(params[0]); 

        return null; 
       } 

       @Override 
       protected void onPostExecute(Void result) 
       { 
        super.onPostExecute(result); 
           imageView.setVisibility(View.VISIBLE); 

       } 

      } 
+0

謝謝KKC!嘗試運行並得到「E/AndroidRuntime(855):java.lang.RuntimeException:無法實例化活動ComponentInfo {com.image/com.image.ImageActivity}:java.lang.NullPointerException」 – user1163234 2012-02-02 10:57:50

0

這裏是如何做到了在最後...

需要使用的AsyncTask我也孫中山的HTML到網頁中爲了檢索從網絡上的實際jpg

String namesTr1 = pb; 
    System.out.println(">>>>NAMEBEFORE"+pb); 
    String nameminus1 = namesTr1.replace(' ','-'); 
    System.out.println(">>>>NAMEAFTERREPLACe"+nameminus1); 
    PGCalc pgcalc = new PGCalc(); 
    String playerhtml="http://www.rotoworld.com/player/nba/1860/"+nameminus1+"/1"; 
    System.out.println(">>>>Before JSOUP"+playerhtml); 

    //Getting html image from Jspoup 
    try { 
     imagehtml = pgcalc.getimage(playerhtml); 
     System.out.println(">>>>>>jsoupimagehtml"+imagehtml); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    //Setting image in Imageview 
    try 
    { 
     URL feedImage = new URL(imagehtml); 
     HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection(); 
     InputStream is = conn.getInputStream(); 
     Bitmap img; 
     return img = BitmapFactory.decodeStream(is); 
     //imageview1.setImageBitmap(img); 

    } 
    catch (MalformedURLException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    return null; 
}