我有一个代码从文件路径解码图像,但是,它占用了主线程中太多的空间和处理。我不知道如何将其改编为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
是什么以及如何将我的位图变量传递给它
答案 0 :(得分:2)
您可以通过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);
}
}
}
答案 1 :(得分:0)
使用此:
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);
}
}
}