显示Android的巨大图片 [英] display huge Images in Android
问题描述
我打算在Android中显示的图像非常大。
我的第一个解决方案 - 以供应为PDF - 失败,因为不是每个手持得到的PDF阅读器preinstalled,我不想要求用户安装一。
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现在的(宽度= 3998px高度= 2827px)我要显示。我下载了这个形象来测试它会显示在画廊。这是相当痛苦的。看来,galery呈现此画面只有一次,如果我放大,我看不懂的文字都没有。
所以我写了testActivity它只是有嵌套在一个LinearLayout中的ImageView的。我把图像转换成可绘制,并将其设置为ImageView的形象来源。
Unforunately应用程序崩溃立刻,由于一个
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"
我没想到的是一个单一的图像可以太大虚拟机的内存
。我打得有点绕,设置 ImageViews
大小 3998及放大器; 2827px
,把图像到 SD卡
和手动读取它的FileInputStream
。
为了我的大惊喜,现在显示了我的形象,但如果我把我的 Nexus S的水平我得到同样的的OutOfMemoryError
和以前一样。照片< BR>
recieving通过的FileInputStream
A位图之间可以somewone点我的主要区别,或将其设置为的ImageView
来源。
另外,我无法滚动的舒适与两家母公司 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);
然后我将它分成四个部分,并用四个ImageViews显示它们。
我试图加载的完整画面,并把它切成四(使用 BitmapFactory.create(位图,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.
所以,我决定用一些BitMa pregionDe codeR:
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屋!