gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:40:14 28 4
gpt4 key购买 nike

我尝试按照此处的教程进行操作: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/

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