Java 在android中更改位图的大小以避免OOM

Java 在android中更改位图的大小以避免OOM,java,android,bitmap,scale,bitmapfactory,Java,Android,Bitmap,Scale,Bitmapfactory,我一直在努力更改从服务器下载的位图的大小,因为我知道这是我收到的OOM错误的问题。我试过下面的其他例子,也试过这个例子,但对于如何使用它以及在哪里使用它,我也不太清楚。我想将位图缩小到屏幕分辨率,但无法解决这个问题。谢谢你在这方面的帮助 同样,这也不是在一个AsyncTask任务中完成的,如果这有区别的话 以下是我设置位图的代码: public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> { B

我一直在努力更改从服务器下载的位图的大小,因为我知道这是我收到的OOM错误的问题。我试过下面的其他例子,也试过这个例子,但对于如何使用它以及在哪里使用它,我也不太清楚。我想将位图缩小到屏幕分辨率,但无法解决这个问题。谢谢你在这方面的帮助

同样,这也不是在一个
AsyncTask
任务中完成的,如果这有区别的话

以下是我设置位图的代码:

public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {

BitmapFactory.Options options = new BitmapFactory.Options();

private final WeakReference<ImageView> imageViewReference;
Resources resources;

public ImageDownloaderTask(ImageView imageView) {
    imageViewReference = new WeakReference<ImageView>(imageView);
}


@Override
protected Bitmap doInBackground(String... params)
{
    return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
    if (isCancelled()) {
        bitmap = null;
        Log.d("HTTPS No go", bitmap.toString());
    }

    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            if (bitmap != null) {
                //Scale the bitmap to a smaller size
                imageView.setImageBitmap(bitmap);
            } else {
                Log.d("Downloading the image: ", "No Image found");
            }
        }

    }
}

//URL connection to download the image
private Bitmap downloadBitmap(String url) {

    HttpURLConnection urlConnection = null;
            try{
            URL uri = new URL(url);
            urlConnection = (HttpURLConnection) uri.openConnection();
            urlConnection.setRequestMethod("GET");
            int statusCode = urlConnection.getResponseCode();
            //check if the HTTP status code is equal to 200, which means that it is ok
            if (statusCode != 200) {
                return null;
            }

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {

                /*
                options.inJustDecodeBounds = true;
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
                int imageHeight = options.outHeight;
                int imageWidth = options.outWidth;
                String imageType = options.outMimeType;
                int sampleSize = calculateInSampleSize(options, imageWidth, imageHeight);
                */
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
            }catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } {}

    return null;
}

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

}
我使用了以下代码:

    Bitmap outBitmap;

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(photoPath, o);

    // The new size we want to scale to ensure memory usage is optimal
    int targetWidth;
    int targetHeight;
    if (o.outHeight > o.outWidth) {
        targetWidth = getResources().getInteger(R.integer.pic_width_px);
        targetHeight = getResources().getInteger(R.integer.pic_height_px);
    } else if (o.outHeight == o.outWidth) {
        targetWidth = targetHeight = getResources().getInteger(R.integer.pic_width_px);
    } else {
        targetWidth = getResources().getInteger(R.integer.pic_width_px);
        targetHeight = getResources().getInteger(R.integer.pic_height_px);
    }

    if (o.outWidth <= targetWidth && o.outHeight <= targetHeight) {
        // Return image as is without any additional scaling
        Bitmap origBitmap = BitmapFactory.decodeFile(photoPath, null);
        outBitmap = Bitmap.createBitmap(origBitmap, 0, 0, o.outWidth, o.outHeight, m, true);
        origBitmap.recycle();

        return outBitmap;
    }

    // Find the correct scale value. It should be the power of 2.
    int scale = 1;
    while(o.outWidth / scale / 2 >= targetWidth &&
            o.outHeight / scale / 2 >= targetHeight) {
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
    scaleOptions.inSampleSize = scale;

    Bitmap scaledBitmap = BitmapFactory.decodeFile(photoPath, scaleOptions);
    return Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), m, true);
位图输出位图;
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(光路,o);
//我们要扩展的新大小,以确保内存使用达到最佳
int targetWidth;
int targetHeight;
如果(外径>外径){
targetWidth=getResources().getInteger(R.integer.pic\u width\u px);
targetSight=getResources().getInteger(R.integer.pic\u height\u px);
}否则,如果(外径==外径){
targetWidth=targetThight=getResources().getInteger(R.integer.pic\u width\u px);
}否则{
targetWidth=getResources().getInteger(R.integer.pic\u width\u px);
targetSight=getResources().getInteger(R.integer.pic\u height\u px);
}
如果(外径=目标光){
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options scaleOptions=新的BitmapFactory.Options();
scaleOptions.inSampleSize=比例;
位图缩放位图=BitmapFactory.decodeFile(光路,缩放选项);
返回Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),m,true);
我使用了以下代码:

    Bitmap outBitmap;

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(photoPath, o);

    // The new size we want to scale to ensure memory usage is optimal
    int targetWidth;
    int targetHeight;
    if (o.outHeight > o.outWidth) {
        targetWidth = getResources().getInteger(R.integer.pic_width_px);
        targetHeight = getResources().getInteger(R.integer.pic_height_px);
    } else if (o.outHeight == o.outWidth) {
        targetWidth = targetHeight = getResources().getInteger(R.integer.pic_width_px);
    } else {
        targetWidth = getResources().getInteger(R.integer.pic_width_px);
        targetHeight = getResources().getInteger(R.integer.pic_height_px);
    }

    if (o.outWidth <= targetWidth && o.outHeight <= targetHeight) {
        // Return image as is without any additional scaling
        Bitmap origBitmap = BitmapFactory.decodeFile(photoPath, null);
        outBitmap = Bitmap.createBitmap(origBitmap, 0, 0, o.outWidth, o.outHeight, m, true);
        origBitmap.recycle();

        return outBitmap;
    }

    // Find the correct scale value. It should be the power of 2.
    int scale = 1;
    while(o.outWidth / scale / 2 >= targetWidth &&
            o.outHeight / scale / 2 >= targetHeight) {
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
    scaleOptions.inSampleSize = scale;

    Bitmap scaledBitmap = BitmapFactory.decodeFile(photoPath, scaleOptions);
    return Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), m, true);
位图输出位图;
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(光路,o);
//我们要扩展的新大小,以确保内存使用达到最佳
int targetWidth;
int targetHeight;
如果(外径>外径){
targetWidth=getResources().getInteger(R.integer.pic\u width\u px);
targetSight=getResources().getInteger(R.integer.pic\u height\u px);
}否则,如果(外径==外径){
targetWidth=targetThight=getResources().getInteger(R.integer.pic\u width\u px);
}否则{
targetWidth=getResources().getInteger(R.integer.pic\u width\u px);
targetSight=getResources().getInteger(R.integer.pic\u height\u px);
}
如果(外径=目标光){
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options scaleOptions=新的BitmapFactory.Options();
scaleOptions.inSampleSize=比例;
位图缩放位图=BitmapFactory.decodeFile(光路,缩放选项);
返回Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),m,true);

我认为在将图像读入内存之前,您需要更改图像的大小

private Bitmap downloadBitmap(String url) {

    HttpURLConnection urlConnection = null;
            try{
            URL uri = new URL(url);
            urlConnection = (HttpURLConnection) uri.openConnection();
            urlConnection.setRequestMethod("GET");
            int statusCode = urlConnection.getResponseCode();
            //check if the HTTP status code is equal to 200, which means that it is ok
            if (statusCode != 200) {
                return null;
            }

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {

                //scale down the image and load
                options.inJustDecodeBounds = true;
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
                options.inSampleSize = calculateInSampleSize(options, 100, 100);
                options.inJustDecodeBounds = false;
                return BitmapFactory.decodeStream(inputStream, null, options);   //I'm not sure here, because the inputStream used twice.
            }
            }catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } {}

    return null;
}

我认为在你将图像读入内存之前,你需要改变图像的大小

private Bitmap downloadBitmap(String url) {

    HttpURLConnection urlConnection = null;
            try{
            URL uri = new URL(url);
            urlConnection = (HttpURLConnection) uri.openConnection();
            urlConnection.setRequestMethod("GET");
            int statusCode = urlConnection.getResponseCode();
            //check if the HTTP status code is equal to 200, which means that it is ok
            if (statusCode != 200) {
                return null;
            }

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {

                //scale down the image and load
                options.inJustDecodeBounds = true;
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
                options.inSampleSize = calculateInSampleSize(options, 100, 100);
                options.inJustDecodeBounds = false;
                return BitmapFactory.decodeStream(inputStream, null, options);   //I'm not sure here, because the inputStream used twice.
            }
            }catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } {}

    return null;
}

谢谢所有帮忙的人。我发现我的问题不是下载位图的大小,因为每次将它们添加到循环视图时,我都会自动调整它们的大小。这是由我在登录页面上播放的gif造成的,它占用了太多的内存,一旦任何其他东西占用了内存,它就会杀死设备


我真的很感谢在座的每一个人,谢谢你们。我自己认为问题在于图像的下载,因为这是常见的罪魁祸首。

感谢所有帮助过我的人。我发现我的问题不是下载位图的大小,因为每次将它们添加到循环视图时,我都会自动调整它们的大小。这是由我在登录页面上播放的gif造成的,它占用了太多的内存,一旦任何其他东西占用了内存,它就会杀死设备


我真的很感谢在座的每一个人,谢谢你们。我认为问题出在图像的下载上,因为这是常见的罪魁祸首。

几天前我也遇到了同样的问题。理论上有两种方法可以做到这一点。要么你们完全下载图像,然后调整图像大小,要么你们必须要求你们的服务器来做这件事,而不是应用程序。我更喜欢第二种解决方案,在那里我发送所需的宽度、高度和所需的图像。服务器计算可能的比例,然后缩小尺寸并返回,打印图像。之后,我只需使用HttpURLConnection下载位图,并从我的连接的输入流中创建位图,没有任何问题


那个么您的错误呢,可能您正在尝试首先从流计算,然后创建它。当然,这会导致崩溃,因为您正试图第二次读取输入流。在读取图像元数据以了解大小时,光标已经移动到流中。现在,当它尝试创建位图时失败,因为它不是从流的第0个字节开始的。但是在中间的某个地方,你的光标最后停了下来。所以,若需要读取两次流,您需要首先将输入流复制到某个地方,以便能够读取两次流。希望对您有所帮助。

几天前我也遇到了同样的问题。理论上有两种方法可以做到这一点。要么你们完全下载图像,然后调整图像大小,要么你们必须要求你们的服务器来做这件事,而不是应用程序。我更喜欢第二种解决方案,在那里我发送所需的宽度、高度和所需的图像。服务器计算可能的比例,然后缩小尺寸并返回,打印图像。之后,我只需使用HttpURLConnection下载位图,并从我的连接的输入流中创建位图,没有任何问题

那个么您的错误呢,可能您正在尝试首先从流计算,然后创建它。当然会的