在 Android 中显示巨大的图像 [英] display huge Images in Android

查看:18
本文介绍了在 Android 中显示巨大的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在 Android 中显示非常大的图像.

我的第一个解决方案 - 以 pdf 格式提供它们 - 失败,因为并非每个手持设备都预装了 pdf 查看器,而且我不想要求用户安装一个.

I'm intending to display very large Images in Android.

My first solution - to supply them as pdf - fails because not every handheld got a pdf-viewer preinstalled, and I don't want to require the users to install one.

所以我现在有一个想要显示的 png (width = 3998px height=2827px).我下载了这张图片来测试它在画廊中的显示方式.这是相当痛苦的.好像画廊只渲染了一次这张图片,如果我放大,我根本看不到文字.

所以我写了一个 testActivity,它只是在一个 LinearLayout 中嵌套了一个 ImageView.我将图像放入 drawable 并将其设置为 ImageView 的图像源.不幸的是,由于

So I have a png now (width = 3998px height=2827px) that I want to display. I downloaded this image to test how it would be displayed the gallery. It was quite painful. It seems that the galery renders this picture only once, and if I Zoom in, I cannot read the text at all.

So I wrote a testActivity which simply has an ImageView nested in a LinearLayout. I put the image into the drawable and set it as ImageView's image-source. Unforunately the app crashes immediatly, due to an "

 ERROR/AndroidRuntime(8906): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget"  

我没想到单个图像对于VM 的内存 来说可能太大.我玩了一会儿,将 ImageViews 大小设置为 3998 &2827px ,将图像放入 sdCard 并使用 fileInputStream 手动读取.
令我大吃一惊的是,它现在显示了我的图像,但是如果我将 Nexus S 水平旋转,我会得到与以前相同的 OutOfMemoryError.

有人可以告诉我通过 FileInputStream 接收位图或将其设置为 ImageView's 源之间的主要区别.

此外,我无法舒适地滚动两个父 scrollViews

我正在寻找一种简单的解决方案来一次显示一个大图像,并且能够水平和垂直滚动同时能够放大和缩小.

I didn't expect that ONE single Image can be too large forVM's memory. I played a little bit around, set ImageViews size to 3998 & 2827px , put the Image to sdCard and read it manually with a fileInputStream.
To my big surprise it now shows my image, but if I turn my Nexus S horizontal I get the same OutOfMemoryError as before.

Can somewone point me the main difference between recieving a Bitmap through a FileInputStream or to set it as ImageView's source.

Also I'm not able to scroll comfortable with two parent scrollViews

I searching for a simple solution to display ONE large image at a time with the ability to scroll horizontal and vertical while able to zoom in and out.

这是我要显示的图像示例

here is a sample of the image I want to display

推荐答案

我知道这是一个旧帖子,但我在这个问题上花了很多时间,所以这是我的解决方案.

I know it's an old post but I spent a lot of time on this problem, so here's my solution.

我想显示一张 2000×3000 的图片,但内存不足或图片太大无法显示.

I wanted to display a 2000×3000 picture but I got out of memory or the image was too large to be displayed.

首先,我得到图片的尺寸:

To begin, I get the dimensions of the picture:

o = new BitmapFactory.Options();
o.inJustDecodeBounds=true;
pictures = BitmapFactory.decodeStream(new FileInputStream(f), null, o);

然后我把它分成四部分,用四个 ImageView 显示出来.我尝试加载完整图片并将其切成四份(使用 BitmapFactory.create(bitmap,int,int,int,int)),但再次出现内存不足.

Then I cut it up into four parts and displayed them with four ImageViews. I tried to load the full picture and cut it into four (using BitmapFactory.create(bitmap,int,int,int,int)) but got out of memory again.

所以我决定使用一些 BitMapRegionDecoder:

So I decided to use some BitMapRegionDecoder:

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
        ImageView iv = new ImageView(this);         
        InputStream istream =   null;
        try {
         istream = this.getContentResolver().openInputStream(Uri.fromFile(f));
        } catch (FileNotFoundException e1) {
         e1.printStackTrace();
        }
        BitmapRegionDecoder decoder     =   null;
        try {
        decoder = BitmapRegionDecoder.newInstance(istream, false);
        } catch (IOException e) {
                    e.printStackTrace();
        }
    int nw = (j*width/k);
    int nh = (i*height/k);

    Bitmap bMap = decoder.decodeRegion(new Rect(nw,nh, (nw+width/k),(nh+height/k)), null);    
    iv.setImageBitmap(bMap);

    }
}

这奏效了.

这篇关于在 Android 中显示巨大的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆