获取asynctask加载位图的问题

时间:2013-07-02 18:44:24

标签: android bitmap android-asynctask

在收到outOfMemory错误后,我想使用AsyncTask加载我的位图,所以我从android开发者网站获得了这个代码示例,并且我在使所有内容协同工作时遇到了一些麻烦。

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

我收到错误:对于BitmapLoader.BitmapWorkerTask类型,方法getResources()未定义。我对android比较新,所以我很可能会使用错误的方法或其他东西,所以我希望有人可以告诉我正确的用法或者至少指出我正确的方向。提前谢谢。

link to the code i copied

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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

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