防止位图太大而无法上传到纹理android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22633638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:16:44  来源:igfitidea点击:

Prevent bitmap too large to be uploaded into a texture android

androidandroid-bitmap

提问by Pratik

I need to display original image in full screen in gallery form. For thumb it will be work perfectly and when I try to display that image in full screen with original source it will not be able to display. In most cases if the image resolution is greater then 2000 then it will display error bitmap too large to be uploaded into a texture android.

我需要以画廊形式全屏显示原始图像。对于拇指,它将完美运行,当我尝试使用原始来源全屏显示该图像时,它将无法显示。在大多数情况下,如果图像分辨率大于 2000,则会显示错误位图太大而无法上传到纹理 android

I want to prevent this, I have search google but not getting any answer regarding this.

我想防止这种情况,我搜索了谷歌,但没有得到任何关于这个的答案。

回答by Phileo99

I came across the same problem and came up with a one liner solution for this problem here:

我遇到了同样的问题,并在这里为这个问题提出了一个单一的解决方案:

Picasso.with(context).load(new File(path/to/File)).fit().centerCrop().into(imageView);

回答by Gilbert92

i just created a if else function to check if the image is bigger than 1M pixels here's the sample code:

我刚刚创建了一个 if else 函数来检查图像是否大于 100 万像素,这是示例代码:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

   if (resultCode == RESULT_OK) {

     if (requestCode == SELECT_PICTURE) {

        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
        int height = bitmap.getHeight(), width = bitmap.getWidth();

        if (height > 1280 && width > 960){
            Bitmap imgbitmap = BitmapFactory.decodeFile(selectedImagePath, options);
            imageView.setImageBitmap(imgbitmap);

            System.out.println("Need to resize");

        }else {
            imageView.setImageBitmap(bitmap);
            System.out.println("WORKS");
        }

回答by Minas

Google provided a training how to do that. Download the sample from Displaying Bitmaps Efficiently

谷歌提供了如何做到这一点的培训。从有效地显示位图下载示例

Take a look to ImageResizer class. ImageResizer.decodeSampledBitmapFrom* use this method to get downscaled image.

看看 ImageResizer 类。ImageResizer.decodeSampledBitmapFrom* 使用此方法获取缩小的图像。

回答by Kartik Nigam

This is the code I used to rectify my problem of fitting an image of size 3120x4196 resolution in an image view of 4096x4096 resolution. Here ImageViewId is the id of the image view created in the main layout and ImageFileLocation is the path of the image which is to be resized.

这是我用来纠正在 4096x4096 分辨率的图像视图中拟合 3120x4196 分辨率图像的问题的代码。这里 ImageViewId 是在主布局中创建的图像视图的 id,ImageFileLocation 是要调整大小的图像的路径。

        ImageView imageView=(ImageView)findViewById(R.id.ImageViewId);          
        Bitmap d=BitmapFactory.decodeFile(ImageFileLcation);
        int newHeight = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
        Bitmap putImage = Bitmap.createScaledBitmap(d, 512, newHeight, true);
        imageView.setImageBitmap(putImage);

回答by Y.S

I found a way to do this without using any external libraries:

我找到了一种不使用任何外部库的方法

if (bitmap.getHeight() > GL10.GL_MAX_TEXTURE_SIZE) {

    // this is the case when the bitmap fails to load
    float aspect_ratio = ((float)bitmap.getHeight())/((float)bitmap.getWidth());
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    (int) ((GL10.GL_MAX_TEXTURE_SIZE*0.9)*aspect_ratio),
                    (int) (GL10.GL_MAX_TEXTURE_SIZE*0.9));
    imageView.setImageBitmap(scaledBitmap);
}
else{

    // for bitmaps with dimensions that lie within the limits, load the image normally
    if (Build.VERSION.SDK_INT >= 16) {
        BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
        imageView.setBackground(ob);
    } else {
        imageView.setImageBitmap(bitmap);
    }
}

Basically, the maximum image dimensions are a system-imposed limit. The above approach will correctly resize bitmaps that exceed this limit. However, only a portion of the entire image will be loaded. To change the region displayed, you can alter the xand yparameters of the createBitmap()method.

基本上,最大图像尺寸是系统强加的限制。上述方法将正确调整超出此限制的位图大小。但是,只会加载整个图像的一部分。要更改显示的区域,您可以更改方法的xy参数createBitmap()

This approach handles bitmaps of any size, including pictures taken with professional cameras.

这种方法可以处理任何尺寸的位图,包括用专业相机拍摄的照片。

References:

参考:

Android Universal Image Loader.

Android 通用图像加载器

回答by shem

You don't need to load the whole image, cause it's too large and probably your phone won't able to show the full bitmap pixels. You need to scale it first according to your device screen size. This is the best method that I found and it works pretty good: Android: Resize a large bitmap file to scaled output file

你不需要加载整个图像,因为它太大了,你的手机可能无法显示完整的位图像素。您需要先根据您的设备屏幕尺寸对其进行缩放。这是我发现的最好的方法,而且效果很好: Android: Resize a large bitmap file to scaled output file