5

When I attempt to load an image from a URL, using the following code (real image path removed):

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://some-path/img.png").getContent());

I receive the following error:

Error reading from ./org/apache/harmony/awt/www/content/image/png.class

Any thoughts on what might be causing the error?

I am using a GoogleTV AVD, if that matters.

4

4 回答 4

4

我希望这已经足够了。

如果您使用的是 php;

echo base64_encode($imgBinary); // You can get the imagebinary by using the fread and fopen methods provided by php

在安卓上:

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet(url));
HttpEntity entity = httpResponse.getEntity();

if(entity != null) {
InputStream is = entity.getContent();
byte[] decodedString = Base64.decode(convertStreamToString(is), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
}

这可能不是最有效的方法,但它应该可以完成这项工作。从那里你可以建立:)

之后,您可以将位图压缩为 PNG,并对其进行保护。例子:

decodedByte.compress(compressformat, quality, stream);//suported compress formats can be used like so: Bitmap.CompressFormat.PNG etc

convertStreamToString 是很容易找到的方法。只需做一个快速的谷歌搜索,或编写自己的。

于 2011-12-02T10:21:10.850 回答
2

试试这个方法:它为我工作这返回位图

bmp=getBitmapFromURL(ur url here);

写这个方法

public static Bitmap getBitmapFromURL(String src) {  
        try {

            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap mybitmap = BitmapFactory.decodeStream(input);

            return mybitmap;

        } catch (Exception ex) {

            return null;
        }
于 2011-12-05T07:11:49.593 回答
0

据我经历过像你这样的场景,我发现图像的输入流无法通过简单的获取流的方法获取尝试更新代码中的以下内容,然后检查结果。我相信你会得到你想要的。

Bitmap bitmap = BitmapFactory.decodeStream(getBitmapStream("http://some-path/img.png"));

这是可以在类中声明的方法,可以在您的解码流方法中直接调用

public InputStream getBitmapStream (String url)
{
    HttpGet httpRequest = null;
    InputStream instream=null;
    try {
        URL bitmapUrl=new URL(url);
            httpRequest = new HttpGet(bitmapUrl.toURI());
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient.execute
    (httpRequest);

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity
    (entity);
            instream = bufHttpEntity.getContent(); 
    } catch (URISyntaxException e) {
            e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return instream;
}
于 2011-12-07T07:37:19.510 回答
0

我的猜测是

URL url = new URL("some url path");
URLConnection urlConnection = url.openConnection();
BitmapDrawable image = new BitmapDrawable(urlConnection.getInputStream());
于 2011-12-08T14:51:27.347 回答