安卓:bitmapfactory.decodestream返回空值

6
我可以帮助您进行翻译。以下是IT技术相关内容:

我尝试从图片路径获取位图图像。但是BitmapFactory.decodeStream返回null值。

代码:

请注意,无需修改HTML标签。
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
connection.disconnect();
input.close();

我在多个网站上搜索,但仍未找到解决方案。


你确定该URL指向BitmapFacotry可以解码的内容吗? - Blackbelt
是的,URL:http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg - Ponmalar
2
有一个 BitmapFactory.decodeStream() 的错误。不要尝试解码流,可以将图像保存在SD卡上,然后通过BitmapFactory加载它或者阅读这篇文章:http://android-developers.blogspot.it/2010/07/multithreading-for-performance.html。 - Blackbelt
5个回答

15

找到了解决方法:

HttpGet httpRequest = new HttpGet(URI.create(path) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
问题在于,一旦你从HttpUrlConnection获得了InputStream,就无法倒回并再次使用相同的InputStream。因此,你必须为实际采样图像创建一个新的InputStream。否则,我们必须中止HTTP请求。

1
这个解决方案与描述的问题有何不同?在两种方法中,我看到您仅使用了一次流。 - Guy
4
@Guy: 只能在http请求中使用一次inputstream,如果你试图下载另一张图片,就会抛出"InputStream already created"的错误。因此,我们需要在下载完成后使用httpRequest.abort()来终止http请求。 - Ponmalar
谢谢。我感激你的回答。 - Guy
此解决方案已经过时,日期为2016年2月22日。 - Philip Enc

1
public Bitmap getBitmapFromUrl(String url)
{
Bitmap bm = null;
InputStream is = null;
BufferedInputStream bis = null;
try 
{
    URLConnection conn = new URL(url).openConnection();
    conn.connect();
    is = conn.getInputStream();
    bis = new BufferedInputStream(is, 8192);
    bm = BitmapFactory.decodeStream(bis);
}
catch (Exception e) 
{
    e.printStackTrace();
}
finally {
    if (bis != null) 
    {
        try 
        {
            bis.close();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    if (is != null) 
    {
        try 
        {
            is.close();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}
return bm;
}

不要忘记在线程内调用此函数(而不是主线程)


1

我也遇到过同样的问题,但我的问题是资源(图片)。请确保图像不是CMYK颜色模式,因为Android不支持CMYK图像。请参阅this question了解更多详细信息。

祝你好运 ;)


0
使用以下代码,我能够从URL下载图像。
String IMAGE_URL = "http://www.kolkatabirds.com/rainquail8vt.jpg";
            //where we want to download it from
            URL url;
            try {
                url = new URL(IMAGE_URL);

                //open the connection
                URLConnection ucon = url.openConnection();

                //buffer the download
                InputStream is = ucon.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is,1024);

                //get the bytes one by one
                int current = 0;

                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }
                //convert it back to an image
                ByteArrayInputStream imageStream = new ByteArrayInputStream(baf.toByteArray());
                Bitmap theImage = BitmapFactory.decodeStream(imageStream);
                img.setImageBitmap(theImage);

ByteArrayBuffer baf = new ByteArrayBuffer(1024); - user1203673
1
什么是ByteArrayBuffer类? - Vladimir Salguero

0

在解码流之前,需要使用BufferedInputStream....

尝试这个方法,对我来说完美运行:

BufferedInputStream buf = new BufferedInputStream(inputsteam, 1024);

将buf传递给解码流,它将完美工作。

Bitmap theImage = BitmapFactory.decodeStream(buf);

最后设置您的位图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接