将大图像加载到位图中?

发布于 2024-09-01 06:26:25 字数 179 浏览 7 评论 0原文

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

它不会给出我觉得奇怪的内存不足错误,但完全相同的代码在较小的图像上运行良好。

通用图库如何在内存如此小的情况下显示来自相机的巨大图片?

I'm trying to make a basic application that displays an image from the camera, but I when I try to load the .jpg in from the sdcard with BitmapFactory.decodeFile, it returns null.

It doesn't give an out of memory error which I find strange, but the exact same code works fine on smaller images.

How does the generic gallery display huge pictures from the camera with so little memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半世晨晓 2024-09-08 06:26:25

尝试设置 inSampleSize,如 这个例子

Try to set the inSampleSize as shown in this example.

我做我的改变 2024-09-08 06:26:25

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

Well after working a lot i found out that the problem was not with code but the ram size of emulator, editing avd and increasing ram size solves all problems and saves huges pics easily. thanks.

浪推晚风 2024-09-08 06:26:25

发送:

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

send:

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

recive:

Bitmap photo = BitmapFactory.decodeByteArray(data.getByteArrayExtra("byte_picture"),0,data.getByteArrayExtra("byte_picture").length);
浪荡不羁 2024-09-08 06:26:25

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

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

Here is another solution that uses inSampleSize and dynamically determines the image resolution to use based on available memory:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文