【问题标题】:Prevent bitmap too large to be uploaded into a texture android防止位图太大而无法上传到纹理android
【发布时间】:2014-05-03 05:46:02
【问题描述】:

我需要以画廊形式全屏显示原始图像。对于拇指来说,它将完美地工作,当我尝试使用原始源全屏显示该图像时,它将无法显示。在大多数情况下,如果图像分辨率大于 2000,则会显示错误bitmap too large to be uploading into a texture android

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

【问题讨论】:

    标签: android android-bitmap


    【解决方案1】:

    我遇到了同样的问题,并为这个问题想出了一个单一的解决方案here

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

    【讨论】:

    • 谢谢,我已经在使用毕加索,所以这很容易解决。
    • @Saragis 不客气,很高兴我能提供帮助。您也可以通过投票支持我的答案来感谢我
    • 为什么您认为这可以解决内存问题? fit()resize() 不会阻止您加载太大的图像。他们只是为您调整图像大小。
    【解决方案2】:

    我刚刚创建了一个 if else 函数来检查图像是否大于 1M 像素,示例代码如下:

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

    【讨论】:

      【解决方案3】:

      Google 提供了如何执行此操作的培训。从Displaying Bitmaps Efficiently下载示例

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

      【讨论】:

      • 在任何地方都看不到这个 ImageResizer
      • @Boy 您是否下载了高效显示位图中提供的示例?
      • com.example.android.displayingbitmaps.util.ImageResizer
      • 对不起,没有下载样例,只是在页面上搜索了imageResizer。除非您编辑当前问题,否则无法撤消 -1。很抱歉
      • @Boy 刚刚编辑了我的答案,以明确您需要先下载示例 :)
      【解决方案4】:

      这是我用来纠正在分辨率为 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);
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

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

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

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

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

          参考资料:

          Android Universal Image Loader.

          【讨论】:

          • 能否告诉我们如何处理 bitmap.getWidth() > GL10.GL_MAX_TEXTURE_SIZE 的情况?
          猜你喜欢
          • 1970-01-01
          • 2012-05-03
          • 2015-04-05
          • 1970-01-01
          • 1970-01-01
          • 2016-11-20
          • 1970-01-01
          • 2012-08-19
          相关资源
          最近更新 更多