从png.class读取时出错

时间:2011-11-29 17:31:33

标签: android google-tv

当我尝试从URL加载图像时,使用以下代码(删除了实际图像路径):

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

我收到以下错误:

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

有关可能导致错误的原因的任何想法?

如果重要的话,我正在使用GoogleTV AVD。

4 个答案:

答案 0 :(得分:4)

我希望这就足够了。

如果您使用的是php;

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

在android上:

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是很容易找到的方法。只需快速谷歌搜索,或自己编写。

答案 1 :(得分: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;
        }

答案 2 :(得分: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;
}

答案 3 :(得分:0)

我的猜测是

URL url = new URL("some url path");
URLConnection urlConnection = url.openConnection();
BitmapDrawable image = new BitmapDrawable(urlConnection.getInputStream());