2013-12-21 66 views
0

我正在開發一個android應用程序,其中我從服務器加載圖像,但是當我這樣做時,android應用程序中沒有圖像(顯示該部分空白)。有人可以告訴我我在做什麼錯?這裏是我正在執行的代碼:從網絡加載圖片時出現問題

public class ad extends Activity { 

ImageView image_view; 
final static String imageLocation="http://example.com/ads/banner320.png"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    setContentView(R.layout.select);  

    image_view = (ImageView)findViewById(R.id.imageView1); 
    loadImage(imageLocation);   

} 
Bitmap bitmap; 
void loadImage(String image_location){ 

    URL imageURL = null; 

    try { 
    imageURL = new URL(image_location); 
    } 

    catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 

    try { 
    HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection(); 
    connection.setDoInput(true); 
    connection.connect(); 
     InputStream inputStream = connection.getInputStream(); 

     bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap 
     image_view.setImageBitmap(bitmap); 
    } 
    catch (IOException e) { 

     e.printStackTrace(); 
    } 
} 

} 

請幫忙通過在此代碼中提出一些更改或錯誤我在做什麼?

回答

0

試試這個..

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    setContentView(R.layout.select);  

    image_view = (ImageView)findViewById(R.id.imageView1); 
    new MyClass().execute(imageLocation);  

} 

class MyClass extends AsyncTask<String, Void, Bitmap> { 

    private Exception exception; 

    protected Bitmap doInBackground(String... imageLocation) { 
     URL imageURL = null; 

    Bitmap bitmap = null; 
    try { 
     imageURL = new URL(utl2); 
    } 

    catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 

    try { 
     HttpURLConnection connection = (HttpURLConnection) imageURL 
       .openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream inputStream = connection.getInputStream(); 

     bitmap = BitmapFactory.decodeStream(inputStream); 

     Log.v("bitmap--", "" + bitmap); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return bitmap; 
    } 

    protected void onPostExecute(Bitmap bitmap) { 
     // TODO: check this.exception 
     // TODO: do something with the feed 

      image_view.setImageBitmap(bitmap); 

} 
} 
1

位圖佔用了大量的內存,所以每次下載相同的圖像是不是好主意,所以最好使用高速緩存功能,這..

如果您需要多次下載相同的圖像,以便將圖像保存在緩存中並稍後使用它。

這裏是best sample example

0

使用該代碼

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

public class LoadImageActivity extends Activity { 

ImageView image_view; 
Button btnLoadImg ; 
    final static String imageLocation="http://www.codeincloud.tk/play.png"; //Use any image location. 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     image_view = (ImageView)findViewById(R.id.imageview); 
     btnLoadImg = (Button)findViewById(R.id.btn_imgload); 

     btnLoadImg.setOnClickListener(loadImage); 
    } 


    View.OnClickListener loadImage = new View.OnClickListener(){ 
    public void onClick(View view) { 
     loadImage(imageLocation); 
      } 
    }; 

    Bitmap bitmap; 
    void loadImage(String image_location){ 

      URL imageURL = null; 

      try { 
      imageURL = new URL(image_location); 
      } 

      catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } 

      try { 
      HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
       InputStream inputStream = connection.getInputStream(); 

       bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap 
       image_view.setImageBitmap(bitmap); 
      } 
      catch (IOException e) { 

       e.printStackTrace(); 
      } 
    } 
    } 

寫在清單文件<uses-permission android:name="android.permission.INTERNET"/>