【问题标题】:how to get the return value from my doInBackground task?如何从我的 doInBackground 任务中获取返回值?
【发布时间】:2017-07-19 18:15:59
【问题描述】:

我是 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;
            }
        }
    }

}

有什么例子吗?

【问题讨论】:

  • 该 url 调用返回什么?它是否返回图片网址?
  • 在 doInBackground 中?我需要将 url2 var 传递给这个函数(这个 var 来自带有 url 的意图),所以我可以在位图中转换它并设置我的 imageview

标签: android android-studio android-asynctask


【解决方案1】:

首先,声明这个 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();
        }
    });


}

希望这可行

【讨论】:

  • 如果代码不起作用,请更改从 url 获取位图的代码。 URL url = new URL(myString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); return myBitmap;
  • 我认为您正在使用 asynctask 在后台从 url 加载图像。而不是你可以使用毕加索图书馆。在您的应用程序 gradle 中添加 compile 'com.squareup.picasso:picasso:2.5.2'。然后Picasso.with(this).load(url2).into(imgDisplay); 您无需将图像转换为位图。此外,通过使用它,您可以缩放您的图像,添加占位符等。阅读square.github.io/picasso
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    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);
         }
    
        }
    

    【讨论】:

      【解决方案4】:

      你应该让 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*/)
      

      【讨论】:

      • 非常感谢!在 void onPostExecute 我尝试添加:imgDisplay.setImageBitmap(result); 但无法解析符号 imgDisplay... 我需要上下文吗?
      • 你需要在类中声明imgDisplay,而不是在方法中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多