2017-02-28 29 views
1

我是新的android工作室,我正在嘗試爲我的網絡操作做一個AsyncTask。如何從我的doInBackground任務獲取返回值?

問題是從它得到的返回變量能夠在imageview中設置圖像。 imgDisplay.setImageBitmap(var)

public class ZoomActivity extends Activity { 


    @SuppressLint("NewApi") 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_zoom); 

     Intent intent = getIntent(); 
     String url2 = intent.getStringExtra("image"); 


     ImageView imgDisplay; 
     Button btnClose; 


     imgDisplay = (ImageView) findViewById(R.id.imgDisplay); 
     btnClose = (Button) findViewById(R.id.btnClose); 


     //Bitmap var = return of doInBackground?????????? 
     imgDisplay.setImageBitmap(var); 


     btnClose.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       ZoomActivity.this.finish(); 
      } 
     }); 


    } 

    private class MyTask extends AsyncTask<String, Integer, String> { 

     @Override 
     protected String doInBackground(String... Params) { 
      String myString = Params[0]; 
      try { 
       URL url = new URL(URL???); //how to pass url2 var here? 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoInput(true); 
       connection.connect(); 
       InputStream input = connection.getInputStream(); 
       Bitmap myBitmap = BitmapFactory.decodeStream(input); 
       return myBitmap; ?????????? 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
    } 

} 

任何示例?

+0

那有什麼網址調用返回?它是否會返回圖片網址? –

+0

in doInBackground?我需要將url2 var傳遞給這個函數(這個var是從一個url的意圖),所以我可以將它轉換成位圖並設置我的imageview –

回答

3

首先,聲明此的AsyncTask類:

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

    @Override 
    protected Bitmap doInBackground(String... strings) { 
     String myString = Params[0]; 
     try { 
      URL url = new URL(myString); 
      Bitmap myBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      return myBitmap; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     super.onPostExecute(bitmap); 
     imgDisplay.setImageBitmap(bitmap); 
    } 
} 

你zoomActivity變化:

public class ZoomActivity extends Activity { 
ImageView imgDisplay; 
@SuppressLint("NewApi") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_zoom); 

    Intent intent = getIntent(); 
    String url2 = intent.getStringExtra("image"); 



    Button btnClose; 


    imgDisplay = (ImageView) findViewById(R.id.imgDisplay); 
    btnClose = (Button) findViewById(R.id.btnClose); 


    //call asynctask 
    new MyTask().execute(url2); 


    btnClose.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      ZoomActivity.this.finish(); 
     } 
    }); 


} 

希望這個作品

+1

如果該代碼不起作用,請更改從url獲取位圖的代碼。 'URL url = new URL(myString); HttpURLConnection連接=(HttpURLConnection)url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); 位圖myBitmap = BitmapFactory.decodeStream(input); return myBitmap;' –

+0

非常感謝,我現在就試試吧! –

+0

完美無缺!謝謝 –

1

當你的doInBackground返回一個對象,這是不言而喻的方法onPostExecute作爲輸入參數,並且該方法在UI線程中執行,而不是並行線程,所以你可以設置IMAG

1

AsyncTask 這本以供參考。 更改您MyTask到

private class MyTask extends AsyncTask<String, Integer, BitMap> { 

    @Override 
    protected Bitmap doInBackground(String... Params) { 
     String myString = Params[0]; 
     try { 
      URL url = new URL(URL???); //how to pass url2 var here? 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; ?????????? 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

protected void onPostExecute(Bitmap result) { 
      //set the Image here. 
imgDisplay.setImageBitmap(result); 
    } 

    } 
1

你應該讓的AsyncTask返回而不是字符串位圖

private class MyTask extends AsyncTask<String, Integer, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(String... Params) { 
     String myString = Params[0]; 
     try { 
      URL url = new URL(myString); //how to pass url2 var here? 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    protected void onPostExecute(Bitmap result) { 
     //set your bitmap here to your imgDisplay 
    } 

} 

然後,你開始任務與

new MyTask().execute(/* urlString*/) 
+0

非常感謝!內部無效onPostExecute我試圖添加:'imgDisplay.setImageBitmap(result);'但無法解析符號imgDisplay ...我需要一個上下文嗎? –

+1

您需要在類中聲明imgDisplay,而不是在方法中聲明。 –

+0

非常感謝! –

相關問題