1

いくつかの画像をダウンロードして、Gallery を使用して表示する必要があります。ギャラリーに使用している画像アダプターの場合、非同期タスクを使用して get view メソッドで画像のダウンロードを開始する必要があります。私の問題は、ダウンロードした画像ビューを呼び出し元の関数に返せないことです。networkonmainthread 例外のため、メイン スレッドを使用してダウンロードできません。

ギャラリー活動

public class GalleryActivity extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.gallery);
        ((Gallery) findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this));
     }

イメージ アダプター

public class ImageAdapter extends BaseAdapter { 

    public View getView(int position, View convertView, ViewGroup parent) {     
        new galleryBackground().execute(Integer.toString(position));
        ImageView i =null;
        return i;
    }

}

ギャラリー

public class galleryBackground extends AsyncTask<String, Integer, String> { 
  Bitmap bm;    
  public String[] myRemoteImages = { ....};
  ImageView i = new ImageView(GalleryActivity.this);

  @Override
  protected String doInBackground(String... arg0) { 
      try { 
          URL aURL = new URL(myRemoteImages[Integer.parseInt(arg0[0])]);
          URLConnection conn = aURL.openConnection();

          conn.connect();
          InputStream is = conn.getInputStream();
          BufferedInputStream bis = new BufferedInputStream(is);
          bm = bitmapFactory.decodeStream(bis);
          bis.close();
          is.close();   
      }

  @Override     
  protected void onPostExecute(String result) {
     i.setImageBitmap(bm);
     i.setScaleType(ImageView.ScaleType.FIT_CENTER);
     i.setLayoutParams(new Gallery.LayoutParams(150, 150));
     // i have to return this Image view to the calling function        
     super.onPostExecute(result);   
}
4

4 に答える 4

2

このライブラリはあなたの問題を解決します:

https://github.com/xtremelabs/xl-image_utils_lib-android

そのリポジトリからプロジェクトにJARをプルします。

アクティビティ/フラグメントでImageLoaderをインスタンス化し、アダプターに渡します。

imageLoader.loadImage(imageView、url)を呼び出すと、すべてがそのシステムによって実行されます。

ウィキはそれを接続する方法をあなたに示すことができます。

于 2013-01-16T14:29:56.803 に答える
1

Asynctask を次のように変更します

AsyncTask<String, Integer, Bitmap>

Bitmap と onPostExecute を返します Bitmap を使用します

yourlist.getItem(your position) and set the bitmap 
于 2013-01-16T11:31:20.813 に答える
1

見てください:ListViewでの画像の遅延ロード

データの表示方法は問題ではありません。アダプターは同じです。

于 2013-01-16T11:24:03.560 に答える
1

doingBackground(); から bm を返す必要があります。

@Override
protected String doInBackground(String... arg0) {
    try{
        URL aURL = new URL(myRemoteImages[Integer.parseInt(arg0[0])]);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = bitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        return bm;
    }
}
于 2013-01-16T11:25:28.257 に答える