doInBackground vazifasidan qaytib qiymatni qanday olish mumkin?

Men Android studiyasida yangi va men Tarmoq operatsiyalari uchun AsyncTask ni bajarishga harakat qilaman.

The problem is to get the return variable from it to be able to set the image in the 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 {

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

}

biron bir misol?

1
doInBackground-da? Url2 ni ushbu funktsiyani bajarishim kerak (bu bor url bilan nishonadir), shuning uchun uni bitmapda o'zgartirishi va mening tasavvurim
qo'shib qo'ydi muallif Rick Joe, manba
Ushbu url chaqiruvi qaytib keladi? Rasm urlini qaytaradimi?
qo'shib qo'ydi muallif Jerin A Mathews, manba

6 javoblar

Birinchidan, bu asynctask sinfini e'lon qiling:

class MyTask extends AsyncTask{

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

Sizning zoomActivity quyidagilarni o'zgartiradi:

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


}

Umid qilamanki bu ish

3
qo'shib qo'ydi
juda minnatdorman, hozir uni sinab ko'raman!
qo'shib qo'ydi muallif Rick Joe, manba
mukammal ishlaydi! rahmat
qo'shib qo'ydi muallif Rick Joe, manba
agar bu kod ishlamasa, bitmapni urldan olish uchun kodni o'zgartiring. URL url = yangi URL (myString); HttpURLConnection aloqasi = (HttpURLConnection) url.openConnection (); connection.setDoInput (rost); connection.connect (); InputStream usuli = connection.getInputStream (); Bitmap myBitmap = BitmapFactory.decodeStream (kirish); myBitmapni qaytarish;
qo'shib qo'ydi muallif Jerin A Mathews, manba
Yordam berishdan xursandman. agar ishlayotgan bo'lsa, javobni qabul qilish.
qo'shib qo'ydi muallif Jerin A Mathews, manba
Sizning foningizda urldan rasm yuklash uchun asynctask dan foydalanmoqdasiz. Buning o'rniga u Picasso kutubxonasidan foydalanishi mumkin. compile 'com.squareup.picasso: picasso: 2.5.2' 'ni qo'shing. keyin Picasso.with (this) .load (url2) .into (imgDisplay); Siz tasvirni bitmapga aylantirishga hojat yo'q. Bundan tashqari siz ur rasmini kengaytira olasiz, to'ldiruvchini qo'shishingiz mumkin. square.github.io/picasso
qo'shib qo'ydi muallif Jerin A Mathews, manba

AsyncTask-ni bir string o'rniga bitmap qaytarishi kerak

private class MyTask extends AsyncTask {

    @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
    }

}

Keyin vazifani boshlaysiz

new MyTask().execute(/* urlString*/)
1
qo'shib qo'ydi
katta rahmat! Ichida void onPostExecute Men: imgDisplay.setImageBitmap (natija); ni qo'shishga harakat qildim, lekin imgDisplay belgisini hal qilolmayman ... kontekst kerakmi?
qo'shib qo'ydi muallif Rick Joe, manba
katta rahmat!
qo'shib qo'ydi muallif Rick Joe, manba
ImgDisplay usulida emas, balki sinfda e'lon qilishingiz kerak.
qo'shib qo'ydi muallif Peter Bruins, manba

AsyncTask-ni bir string o'rniga bitmap qaytarishi kerak

private class MyTask extends AsyncTask {

    @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
    }

}

Keyin vazifani boshlaysiz

new MyTask().execute(/* urlString*/)
1
qo'shib qo'ydi
katta rahmat! Ichida void onPostExecute Men: imgDisplay.setImageBitmap (natija); ni qo'shishga harakat qildim, lekin imgDisplay belgisini hal qilolmayman ... kontekst kerakmi?
qo'shib qo'ydi muallif Rick Joe, manba
katta rahmat!
qo'shib qo'ydi muallif Rick Joe, manba
ImgDisplay usulida emas, balki sinfda e'lon qilishingiz kerak.
qo'shib qo'ydi muallif Peter Bruins, manba

DoInBackground ob'ektni qaytarsa, u

1
qo'shib qo'ydi

AsyncTask This this for reference. Change you MyTask to

 private class MyTask extends AsyncTask {

    @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
qo'shib qo'ydi

AsyncTask This this for reference. Change you MyTask to

 private class MyTask extends AsyncTask {

    @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
qo'shib qo'ydi