无法使用异步任务将图像加载到imageview中

时间:2014-01-02 00:20:01

标签: java android bitmap android-asynctask

我尝试按照这里的教程:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html制作了以下BitmapWorkerTask.java文件:

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) {
    Log.d("NICK","doInBackground");

    data = params[0];
    return decodeSampledBitmapFromResource(getResources(), data, 100, 100);//**changed
}

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

    Log.d("NICK","onPostExecute");
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}


}

我不得不改变行

return decodeSampledBitmapFromResource(getResources(), data, 100, 100);

return decodeSampledBitmapFromResource(System.getResources(), data, 100, 100);

因为它告诉我无法解析方法getResources()。

当我尝试实际使用此图像时,我的图像视图永远不会将图像设置为它,它仍为空。当我使用需要加载的ImageView打开活动时,我的输出日志显示了这一点:(loadBitmap记录在loadBitmap函数内的活动中)

01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.225: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.225: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.355: INFO/System.out(32367): PackageUpdatedListener Data ::    package:com.nick.simplequiz.paid
01-01 19:15:38.626: WARN/ActivityManager(653): Activity pause timeout for A  ActivityRecord{656f0688 u0 com.nick.simplequiz.paid/.TabletGallery t556}
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute

这表明从未到达onPostExecute中ifView语句设置为图像的内部。我怀疑这是导致imageview永远不会显示图像的原因,但我不知道如何正常工作。任何建议都非常感谢。

1 个答案:

答案 0 :(得分:1)

试试这个,在主活动类的顶部,在onCreate方法之外,定义一个名为res的资源类型的变量,然后在你的onCreate中,比如res = getResources();.然后在decodeSampled方法中,使用res。试试吧。 :)