gpt4 book ai didi

java - 如何在Android中的AsyncTask中应用此代码?

转载 作者:行者123 更新时间:2023-12-02 04:47:56 25 4
gpt4 key购买 nike

我有一个从文件路径解码图像的代码,但是,它占用了太多的空间并在主线程中进行处理。我不知道如何将其改编成 AsyncTask 类。

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

我想使用 Android Developer 中找到的代码

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

代码

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

我不知道 resId 是什么以及如何将我的位图变量传递给它

最佳答案

您可以通过Async_BitmapWorkerTask类中的构造函数传递参数。您可能想阅读更简单的 AsyncTask 示例,例如 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);
}
}
}

关于java - 如何在Android中的AsyncTask中应用此代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29508739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com