2

After getting an outOfMemory error I wanted to load my bitmaps using an AsyncTask so I got this code sample from the android developer website and I'm having some trouble getting everything working together.

public class BitmapLoader {

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

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(String orgImagePath,
        int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(orgImagePath, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(orgImagePath, options);
}

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

I am getting an error: the method getResources() is undefined for the type BitmapLoader.BitmapWorkerTask. I'm relatively new to android so its very possible that I'm using the wrong method or something so I was hoping someone could enlighten me on the correct usage or at least point me in the right direction. Thanks in advance.

link to the code i copied

4

2 回答 2

3

您需要活动上下文。将活动上下文传递给 BitmapLoader 的构造函数。

     public class BitmapLoader {
     Context context; 
     public BitmapLoader(Context mContext)
     {
       context = mContext;
     } 
     }

利用context.getResources()

检查以下链接

http://developer.android.com/reference/android/view/ContextThemeWrapper.html#getResources()

于 2013-07-02T18:46:36.463 回答
0

嗨,我有同样的问题,我以同样的方式解决了它。但是我使用了 FilePath,就像你需要的那样。这是我的解决方案。你只需要

new BitmapWorkerTask(this, imageView, imageName).execute();

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;
    private String fName;

    public BitmapWorkerTask(Context context, ImageView imageView, String res) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
        fName = new File(context.getFilesDir(), res).getAbsolutePath();
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... args) {

    return decodeSampledBitmapFromResource(fName, 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);
            }
        }
    }
}

public static Bitmap decodeSampledBitmapFromResource(String fName) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    //BitmapFactory.decodeStream(is, padding, options);
    BitmapFactory.decodeFile(fName, options); ...... The rest is the same

唯一的区别是文件“new File(context.getFilesDir(), res).getAbsolutePath();”的 Pah

于 2013-08-12T13:49:56.090 回答