简体   繁体   中英

How to apply this code in AsyncTask in Android?

I have a code that decodes an image from a filepath, however, it takes up too much space and processing in the main thread. I have no idea how to adapt this into an AsyncTask class.

Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
SetBitmapOptions(bitmapOptions);
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byte_arr = stream.toByteArray();
image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
viewImage.setImageBitmap(bitmap);

I wanted to use the code found here in Android Developer

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

Code

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;
    private int data = 0;

public BitmapWorkerTask(ImageView imageView) {
    // Use a WeakReference to ensure the ImageView can be garbage collected
    imageViewReference = new WeakReference<ImageView>(imageView);
}

// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
    data = params[0];
    return decodeSampledBitmapFromResource(getResources(), data, 100, 100));
}

// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
    if (imageViewReference != null && bitmap != null) {
        final ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}
}

I have no idea what resId is and how to pass my bitmap variable to it

You can pass parameters through the constructor in the Async_BitmapWorkerTask class. You may want to read up on simpler AsyncTask Examples such as this example .

someMethod() {
    Bitmap bitmap;
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    SetBitmapOptions(bitmapOptions);
    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);

    // Run the AsyncTask with the bitMap and imageview as a parameters
    new Async_BitmapWorkerTask(bitmap, imageView).execute();
}

class Async_BitmapWorkerTask extends AsyncTask<Integer, Void, String> {
    private final Bitmap bitmap;
    private final ImageView imageView;
    private int data = 0;

    // Constructor
    public Async_BitmapWorkerTask(Bitmap bitmap, ImageView imageView) {
        this.bitmap = bitmap;
        this.imageView = imageView;
    }

    // Compress and Decode image in background.
    @Override
    protected String doInBackground(Integer... params) {

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] byte_arr = stream.toByteArray();
        String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);

        return image_str;
    }

    // This method is run on the UI thread
    @Override
    protected void onPostExecute(String string) {
        if (imageView != null && bitmap != null) {
            imageView.setImageBitmap(bitMap);
        }
    }
}

Use this :

    class LoadImage extends AsyncTask<Object, Void, Bitmap>{

    private ImageView imv;
    private String path;

    public LoadImage(ImageView imv) {
         this.imv = imv;
         this.path = imv.getTag().toString();
    }

@Override
protected Bitmap doInBackground(Object... params) {
    Bitmap bitmap = null;
    File file = new File( 
            Environment.getExternalStorageDirectory().getAbsolutePath() + path);

    if(file.exists()){
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    }

    return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
    if (!imv.getTag().toString().equals(path)) {
           /* The path is not same. This means that this
              image view is handled by some other async task. 
              We don't do anything and return. */
           return;
    }

    if(result != null && imv != null){
        imv.setVisibility(View.VISIBLE);
        imv.setImageBitmap(result);
    }else{
        imv.setVisibility(View.GONE);
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM