加载大图从服务器上的Andr​​oid [英] Load Large Image from server on Android

查看:91
本文介绍了加载大图从服务器上的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示来自服务器的JPG文件到一个ImageView的。当我尝试加载一个较小的图像(300x400),没有问题。但是,当我尝试加载一个完整大小的图片(2336x3504),图像将不会加载。图像的文件大小只有2MB。我不明白在logcat中的任何错误,也没有抛出异常。它根本不会加载图像。我也尝试过使用这样的:

I am trying to display a jpg file from a server into an imageView. When I try to load a smaller image (300x400), there are no problems. But when I try to load a full size picture (2336x3504), the image will not load. The file size of the image is only 2mb. I do not get any errors in logcat and there are no exceptions thrown. It simply won't load the image. I also tried using this:

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

这并没有做任何事情来帮助装载大文件,但它并调整较小的图像(如它是假设)。我做了大的图片添加到我的资源,并对其进行了测试,就好像它是嵌入在应用程序,它工作得很好,只是不会在服务器上运行。我一直在做这一切的一天,似乎无法弄清楚如何加载这些大型图片。任何人都可以帮助我吗?感谢您的任何信息。

This doesn't do anything to help load the large files, but it does resize the smaller image (like it is suppose to). I did add the large picture to my resources and tested it as if it was embedded in the app and it worked fine, just won't work on the server. I have been working all day on this and can't seem to figure out how to load these large pictures. Can anyone help me out with this? Thanks for any info.

<一个href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">Here就是在那里,我发现上面的code和一直在玩其他的例子,但还没有得到它的工作的联系。

Here is the link where I found the above code and have been playing with the other examples but still not getting it to work.

编辑:

下面是code我使用,以加载图像:

Here is the code I'm using, to load the image:

public static Bitmap getBitmapFromURL(String src) {
    Bitmap bmImg;
    URL myFileUrl = null;

    try {
        myFileUrl = new URL(src);

        HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 16;

        bmImg = BitmapFactory.decodeStream(is, null, options);
        return bmImg;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("Error", e.toString());
        return null;
    }
}

下面是logcat的截图(无法弄清楚如何复制文本适当地蚀)我清除日志之前,我按下按钮来加载图像。因此,所有你看到的是,当我打的按钮,会发生什么。我擦除公司和应用程序名称(这里可以看到融为一体,承担其com.mycompany.myapp。

Here is the logcat screenshot (couldn't figure out how to copy the text appropriately in eclipse) I cleared the log right before I hit the button to load the image. So all you see is what happens when I hit that button. I erased the company and app names (where you see "com.", assume its "com.mycompany.myapp".

推荐答案

这是不寻常的 BitmapFactory.de codeFromStream()放弃,只是回报时将其直接连接远程连接到的InputStream 。在内部,如果没有提供的BufferedInputStream 的方法,将包装提供的流于一体的具有16384一种选择,有时工作缓冲区的大小是通过一个的BufferedInputStream 具有较大的缓冲区大小,如:

It is not uncommon for BitmapFactory.decodeFromStream() to give up and just return null when you connect it directly to the InputStream of a remote connection. Internally, if you did not provide a BufferedInputStream to the method, it will wrap the supplied stream in one with a buffer size of 16384. One option that sometimes works is to pass a BufferedInputStream with a larger buffer size like:

BufferedInputStream bis = new BufferedInputStream(is, 32 * 1024);

一个更普遍有效的方法是要下载的文件完全第一,然后去$ C C这样的数据$:

A more universally effective method is to download the file completely first, and then decode the data like this:

InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8190);

ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
    baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

仅供参考,缓冲区大小在这个例子有些武断。正如在其他的答案已经说了,这是一个奇妙的想法不是保持图像的内存大小的时间比你不得不这样做。你可能会考虑直接写入到一个文件,并显示下采样版本。

FYI, the buffer sizes in this example are somewhat arbitrary. As has been said in other answers, it's a fantastic idea not to keep an image that size in memory longer than you have to. You might consider writing it directly to a file and displaying a downsampled version.

希望帮助!

这篇关于加载大图从服务器上的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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