Android 如何将BitmapFactory.Option设置为位图对象?

Android 如何将BitmapFactory.Option设置为位图对象?,android,bitmap,ram,Android,Bitmap,Ram,我有一个函数,它使用文件路径访问SD卡上的JPG文件,并创建该文件的位图。它使用inSampleSize制作与RAM兼容的位图: public static Bitmap decodeSampledBitmapFromResource(String imgPath, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions fin

我有一个函数,它使用文件路径访问SD卡上的JPG文件,并创建该文件的位图。它使用inSampleSize制作与RAM兼容的位图:

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

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imgPath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(imgPath, options);
    }

现在我想向该函数传递位图,而不是像这样的imgPath:

public static Bitmap decodeSampledBitmapFromResource(Bitmap bitmap, int reqWidth, int reqHeight) 

我想这样做是因为我在屏幕上有多个位图。我可以使用大的临时位图,但由于内存问题,我无法将其添加到位图列表中。BitmapFactory没有此选项。执行此操作的最佳方法是什么?

“现在我想将位图传递给此函数,而不是imgPath”--为什么?内存中已经有了
位图。如果要创建它的缩放版本,请使用。@Commonware InSampleSize是一个依赖于屏幕的数字。我无法用createScaledBitmap()计算它。那么?如果使用
createScaledBitmap()。您只需要所需位图的大小。在将这些值传递到
calculateInSampleSize()
中时,您已经知道了这一点。