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

问题描述

我正在尝试将来自服务器的 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.

这里是我在上面找到的链接代码,并一直在玩其他示例,但仍未使其正常工作.

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.

这是我正在使用的代码,用于加载图像:

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 屏幕截图(无法弄清楚如何在 eclipse 中适当地复制文本)我在点击按钮加载图像之前清除了日志.所以你所看到的就是当我按下那个按钮时会发生什么.我删除了公司和应用程序名称(在您看到com."的地方,假设它是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.decodeFromStream() 放弃并只返回 null 的情况并不少见将其直接连接到远程连接的 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);

更普遍有效的方法是先完整下载文件,然后像这样解码数据:

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.

希望有帮助!

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

06-03 13:39
查看更多