2017-02-28 31 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

非常感谢! –

相关问题