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

标签 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 永远不会设置图像,它仍然是空的。当我使用需要加载的 ImageView 打开 Activity 时,我的输出日志显示了这一点:(loadBitmap 记录在我的 Activity 中的 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 中将 imageView 设置为图像的 if 语句内部。我怀疑这就是导致 ImageView 永远不显示图像的原因,但我不知道如何使其正常工作。非常感谢任何建议。

最佳答案

尝试一下,在主 Activity 类的顶部,在 onCreate 方法之外,定义一个名为 res 的资源类型变量,然后在 onCreate 中,说 res = getResources();。然后在decodeSampled方法中,使用res.尝试一下。 :)

关于java - 无法使用异步任务将图像加载到 imageview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20874677/

相关文章:

java - 自 JRE 8u232 起,JNI 无法在签名的 JAR 中找到类

java - 是否有任何 "modern"框架支持在编译时检查数据绑定(bind)?

java - Apache POI - 允许使用时间字符串进行计算

java - Android - 可运行对象解析为 runOnUiThread() 参数

android - Android 2.3 相机方向不变

android - 位图太大

java - 如何指定用户是否已共享我的应用程序 x 次?

java - Eclipse 中的编译器合规性错误

java - 使用 JNI 将图像从 C++ 发送到 Java

macos - NSImage 和 PDFImageRep 缓存仍然仅以一种分辨率绘制