Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中加载大图像_Java_Android_Image - Fatal编程技术网

Java 在Android中加载大图像

Java 在Android中加载大图像,java,android,image,Java,Android,Image,众所周知,Android在RAM内存中存储位图数据时存在问题。我需要在视图上加载一张图像(来自13Mpx相机的照片),然后我需要能够放大和缩小图像。图像应该是可变的。现在是这样实施的: BitmapFactory.Options options = new BitmapFactory.Options(); options.inMutable = true; _bitmap = BitmapFactory.decodeFile(_path, options); 当我拍摄一张大照片(13或8 M

众所周知,Android在RAM内存中存储位图数据时存在问题。我需要在视图上加载一张图像(来自13Mpx相机的照片),然后我需要能够放大和缩小图像。图像应该是可变的。现在是这样实施的:

BitmapFactory.Options options =  new BitmapFactory.Options();
options.inMutable = true;
_bitmap = BitmapFactory.decodeFile(_path, options);
当我拍摄一张大照片(13或8 Mpx)时,程序因“内存不足”错误而崩溃。我需要一些解决这个问题的办法。我需要一些类,可以加载和操作(缩放它)与大图像。它需要等于或小于API-8


我尝试了Universall图像加载程序,但它没有缩放选项。有人知道如何解决这个问题吗?

我建议使用Square的library。用于图像缩放。

我建议使用Square中的library。用于图像缩放。

尝试此方法缩放位图

final BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inJustDecodeBounds = true;

                            /*
                             * If set to a value > 1, requests the decoder to
                             * subsample the original image, returning a smaller
                             * image to save memory. The sample size is the
                             * number of pixels in either dimension that
                             * correspond to a single pixel in the decoded
                             * bitmap. For example, inSampleSize == 4 returns an
                             * image that is 1/4 the width/height of the
                             * original, and 1/16 the number of pixels. Any
                             * value <= 1 is treated the same as 1. Note: the
                             * decoder uses a final value based on powers of 2,
                             * any other value will be rounded down to the
                             * nearest power of 2.
                             */
                            options.inSampleSize = 2;

                            // Decode bitmap with inSampleSize set
                            options.inJustDecodeBounds = false;

                            bitmap = BitmapFactory.decodeFile(path, options);
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
/*
*如果设置为大于1的值,则请求解码器
*对原始图像进行子采样,返回较小的
*图像以节省内存。样本量为
*任意维度中的像素数
*对应于解码图像中的单个像素
*位图。例如,inSampleSize==4返回一个
*图像的宽度/高度为图像宽度/高度的1/4
*原始,1/16像素数。任何

*值尝试此方法缩放位图

final BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inJustDecodeBounds = true;

                            /*
                             * If set to a value > 1, requests the decoder to
                             * subsample the original image, returning a smaller
                             * image to save memory. The sample size is the
                             * number of pixels in either dimension that
                             * correspond to a single pixel in the decoded
                             * bitmap. For example, inSampleSize == 4 returns an
                             * image that is 1/4 the width/height of the
                             * original, and 1/16 the number of pixels. Any
                             * value <= 1 is treated the same as 1. Note: the
                             * decoder uses a final value based on powers of 2,
                             * any other value will be rounded down to the
                             * nearest power of 2.
                             */
                            options.inSampleSize = 2;

                            // Decode bitmap with inSampleSize set
                            options.inJustDecodeBounds = false;

                            bitmap = BitmapFactory.decodeFile(path, options);
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
/*
*如果设置为大于1的值,则请求解码器
*对原始图像进行子采样,返回较小的
*图像以节省内存。样本量为
*任意维度中的像素数
*对应于解码图像中的单个像素
*位图。例如,inSampleSize==4返回一个
*图像的宽度/高度为图像宽度/高度的1/4
*原始,1/16像素数。任何

*值位图每像素占用4字节==>13MP等于52MB内存。 您应该首先使用BitmapFactory.Options来获取大小。通过使用
inJustDecodeBounds
可以获得位图对象及其所有元数据,但没有实际图像

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
然后计算屏幕所需的比例大小:

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;
}
并使用它加载位图的缩放版本:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

这一切都在

中解释过,位图每像素占用4字节==>13MP等于52MB内存。 您应该首先使用BitmapFactory.Options来获取大小。通过使用
inJustDecodeBounds
可以获得位图对象及其所有元数据,但没有实际图像

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
然后计算屏幕所需的比例大小:

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;
}
并使用它加载位图的缩放版本:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
这些都在报告中解释过了

要避免
java.lang.OutOfMemory
异常,请在解码位图之前检查位图的尺寸,除非您完全信任源为您提供可预测大小的图像数据,这些数据可以轻松地放入可用内存中


要避免
java.lang.OutOfMemory
异常,请在解码位图之前检查位图的尺寸,除非您完全信任源为您提供可预测大小的图像数据,这些数据可以轻松地放入可用内存中

非常感谢大家给了我们所有的答案

我认为我应该使用inJustDecodeBounds选项并加载调整大小的位图,但需要有经验的人对这个问题有一个新的看法

我还考虑使用Universal Image Loader加载和缓存大型图像,以及使用ScaleImageView之类的工具进行缩放


有一种方法可以解决我的问题。部分加载位图

非常感谢大家给了我们所有的答案

我认为我应该使用inJustDecodeBounds选项并加载调整大小的位图,但需要有经验的人对这个问题有一个新的看法

我还考虑使用Universal Image Loader加载和缓存大型图像,以及使用ScaleImageView之类的工具进行缩放

有一种方法可以解决我的问题。部分加载位图

可能的重复可能的重复