【问题标题】:Load large images into Bitmap?将大图像加载到位图中?
【发布时间】:2011-02-17 10:21:03
【问题描述】:

我正在尝试制作一个显示来自相机的图像的基本应用程序,但是当我尝试使用 BitmapFactory.decodeFile 从 sdcard 加载 .jpg 时,它返回 null。

它不会给出我觉得奇怪的内存不足错误,但完全相同的代码在较小的图像上也能正常工作。

通用图库如何以如此少的内存显示来自相机的巨大图片?

【问题讨论】:

    标签: android bitmap jpeg image


    【解决方案1】:

    尝试设置inSampleSize如this example所示。

    【讨论】:

    • 您甚至可以尝试动态选择inSampleSize,首先使用inJustDecodeBounds 并将其与您的可用空间进行比较。
    【解决方案2】:

    您可以按照这个惊人的教程进行操作

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    【讨论】:

      【解决方案3】:

      经过大量工作后,我发现问题不在于代码,而在于模拟器的 ram 大小,编辑 avd 和增加 ram 大小可以解决所有问题并轻松保存巨大的图片。谢谢。

      【讨论】:

        【解决方案4】:

        发送:

                                BitmapFactory.Options o = new BitmapFactory.Options(); 
                            o.inJustDecodeBounds = true; 
                            BitmapFactory.decodeFile(filePath, o); 
        
                            int REQUIRED_SIZE = 640; 
                            int width_tmp = o.outWidth, height_tmp = o.outHeight; 
                            int scale = 1; 
                            while(true) { 
                                if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) break; 
                                width_tmp /= 2; 
                                height_tmp /= 2; 
                                scale *= 2; 
                            } 
        
                            BitmapFactory.Options o2 = new BitmapFactory.Options(); 
                            o2.inSampleSize = scale; 
                            Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2); 
        
                            ByteArrayOutputStream bs2 = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.PNG, 90, bs2);
                            getIntent().putExtra("byte_picture", bs2.toByteArray());
        

        接收:

        Bitmap photo = BitmapFactory.decodeByteArray(data.getByteArrayExtra("byte_picture"),0,data.getByteArrayExtra("byte_picture").length);
        

        【讨论】:

          【解决方案5】:

          这是另一个使用 inSampleSize 并根据可用内存动态确定要使用的图像分辨率的解决方案:

          http://bricolsoftconsulting.com/handling-large-images-on-android/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多