如何从我的doInBackground任务中获取返回值?

时间:2017-02-28 16:53:20

标签: android android-studio android-asynctask

我是android studio的新手,我正在尝试为我的网络操作执行AsyncTask。

问题是从中获取返回变量,以便能够在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;
            }
        }
    }

}

任何例子?

4 个答案:

答案 0 :(得分: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 :(得分:1)

当你的doInBackground返回一个对象时,它会转到方法onPostExecute作为输入参数,并且该方法在UI线程中执行而不是并行线程,因此你可以设置imag

答案 2 :(得分: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);
     }

    }

答案 3 :(得分:1)

你应该让AsyncTask返回一个Bitmap而不是一个String

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*/)