【问题标题】:Problems getting asynctask to load bitmap让异步任务加载位图的问题
【发布时间】:2023-04-04 16:21:01
【问题描述】:

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

我收到一个错误:方法 getResources() 未为 BitmapLoader.BitmapWorkerTask 类型定义。我对android比较陌生,所以很可能我使用了错误的方法或其他东西,所以我希望有人能启发我正确的用法,或者至少为我指明正确的方向。提前致谢。

link to the code i copied

【问题讨论】:

    标签: android bitmap android-asynctask


    【解决方案1】:

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

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

    使用context.getResources()

    查看以下链接

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

    【讨论】:

    • 谢谢,你太棒了!您认为您可以帮助我尝试将 sdcard 中的图像作为资源而不是 res 文件夹传递吗?
    • 你需要从 sdcard 获取图片的路径。搜索一下,你会发现很多类似的帖子。如果不问一个新问题。我相信社区会帮助你。
    【解决方案2】:

    您好,我遇到了同样的问题,我以同样的方式解决了它。但是我使用了 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多