0

こんにちは、AsyncTask を介して URL リンクから画像を取得しようとしています。画像自体を取得する機能は正常に動作します。しかし、私がやろうとしているのは、src変数をasyncTaskに渡すことですが、これは私にとってはうまくいかないようです。リターンは空白になります。

コードは次のとおりです。

 private AsyncTask<String, Void, Drawable> task2;
 Drawable profile;
 public Drawable getProfile(String src){        
    task2 = new AsyncTask<String, Void, Drawable>() {           
        ProgressDialog dialog2;
        InputStream is;
        Drawable d;
        @Override 
        protected void onPreExecute(){
            dialog2 = new ProgressDialog(Thoughts.this, ProgressDialog.STYLE_SPINNER);
            dialog2.setMessage("Loading Data...");          
            dialog2.setCancelable(false);
            dialog2.setCanceledOnTouchOutside(false);   
            dialog2.show();
        }
        @Override
        protected Drawable doInBackground(String... src) {
                try
                {
                    is = (InputStream) new URL(src[0]).getContent();
                    d = Drawable.createFromStream(is, "src name");
                    return d;
                }catch (Exception e) {
                    e.toString();
                    return null;
                }
        }
        @Override
        protected void onPostExecute(Drawable result2) {
            profile = result2; 
            dialog2.dismiss();
        }
    };
    task2.execute(src);
    return profile;
}

そして、onCreate(); でこのように呼び出します。

Drawable p4 = getProfile("http://..../xyz.jpg");
Drawable p5 = getProfile("http://..../xyz.jpg");

ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
ImageView thoughtsProfilePic1 =(ImageView) findViewById(R.id.ivProfilePicData1);

thoughtsProfilePic.setImageDrawable(p4);
thoughtsProfilePic1.setImageDrawable(p5);
4

2 に答える 2

1

AsyncTask非同期ジョブを実行するのに役立ちます。Drawable あなたのコードでは、呼び出した直後に戻ってくることがわかります。しかし、その時点では、あなたの asynctask はまだ完了しておらず、ドローアブルはまだ null です。

task2.execute(src);
return profile;

asynctask でジョブが完了したときにドローアブル リソースを設定する場合は、ImageViewメソッドに追加するだけです。そのはず:

  public void getProfile(String src, final ImageView v){           

         @Override
    protected void onPostExecute(Drawable result2) {

        // set drawable for ImageView when complete.
        v.setImageDrawable(result2);
        dialog2.dismiss();
    }
    task2.excute(src);
    //do not need return anything.
  } 

これを使って:

 ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
 getProfile("http://..../xyz.jpg", thoughtsProfilePic );

この助けを願っています。

更新:
非同期メソッドから直接値を返す方法はありません。別の選択肢があります。まず、ジョブが完了したときに通知するためのインターフェイスを作成します。

 public interface INotifyComplete{  
      public void onResult(Drawable result);  
 } 

次に、アクティビティ クラスは次のようになります。

 public class YourActivity extends Activity implement INotifyComplete{
 private Drawable res1;
 private Drawable res2;
 public void onResult(Drawable result){
    if(result == res1){
        // do something with resource 1
        ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
        thoughtsProfilePic.setImageDrawable(result);
    }
    if(result == res2){
        // do something with resource 2
    }
  }

 void someMethod(){
// you can use this way to call
    getProfile("http://..../xyz.jpg", res1, YourActivity.this);
    //or this
    getProfile("http://..../xyz.jpg", res2, new INotifyComplete(){
        public void onResult(Drawable result){
            // result is res2, so don't need to check
        }
    });
 }
 public void getProfile(String src, final Drawable res, final INotifyComplete notify){
   //don't need store asynctask instance
    AsyncTask<String, Void, Drawable>  task2 = new AsyncTask<String, Void, Drawable>(){

        // do something ...

        protected void onPostExecute(Drawable result2) {             
        dialog2.dismiss();
        // you will set the value to your drawable then notify it when complete job
        res = result2;
        notify.onResult(res);
    }
    }
    task2.excute();
}
  }
于 2013-01-12T09:57:37.087 に答える
0

ドローアブルを返すアクティビティにパブリックメンバー関数を記述し、asyncTask クラスの doInBackground() メソッドで関数を呼び出すだけです。

Drawable downloadImage(){
//write code here to download image so you can return any dataType
}

doInBackground() メソッドでこの関数を呼び出し、返された結果をアクティビティの変数に保存するだけです。

お気に入り

void doInBackground(){
    drawableVariable = downloadImage();    
}

編集: asyncTask はバックグラウンド スレッドであり、UI スレッドではないため、UI 作業を行う場合は、runOnUiThread() メソッドによって UI スレッドでその作業を実行する必要があります。

runOnUiThread(new Runnable() {
           @Override
           public void run() {

               /* update your UI here for example what you are doing something */;
               profile = result2;
           }
       });
于 2013-01-12T10:05:53.693 に答える