6

我试图从图像的路径中获取位图图像。但BitmapFactory.decodeStream返回null值。

代码:

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();

我搜索了更多网站,但我仍然没有得到解决方案。

4

5 回答 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();

问题是一旦你使用了InputStreamfrom a HttpUrlConnection,你就不能倒带并InputStream再次使用它。因此,您必须为InputStream图像的实际采样创建一个新的。否则我们必须中止HTTP请求。

于 2012-05-18T12:19:31.873 回答
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;
}

不要忘记在线程(不是主线程)中调用它

于 2012-05-18T10:36:33.043 回答
1

我有同样的问题,但在我的情况下,问题是资源(图像)。确保图像不是 CMYK 颜色模式,因为 Android 不支持 CMYK 图像。有关更多详细信息,请参阅此问题

祝你好运 ;)

于 2014-01-15T19:06:25.283 回答
0

使用以下代码 iam 能够从 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);
于 2012-05-18T10:14:17.283 回答
0

BufferedInputStream 在解码流之前是必需的......

试试这个,它非常适合我使用;

BufferedInputStream buf = new BufferedInputStream(inputsteam, 1024);

将 buf 传递给解码流,它将完美运行。

Bitmap theImage = BitmapFactory.decodeStream(buf);

最后设置你的位图。

于 2013-06-18T17:02:29.533 回答