从 png.class 读取时出错

发布于 2024-12-18 18:12:50 字数 352 浏览 0 评论 0原文

当我尝试使用以下代码从 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,如果这很重要的话。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别低头,皇冠会掉 2024-12-25 18:12:50

我希望这已经足够了。

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

I hope this will be sufficient.

If you are using php;

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

on 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); 
}

This is probably not the most efficient way, but it should do the job.
From there on you can build :)

You can compress the bitmap into a PNG after, and safe it. example:

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

convertStreamToString are easily found methods. Just do a quick google search, or write your own.

烂人 2024-12-25 18:12:50

尝试这个方法:它对我有用这返回位图

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;
        }

Try this method: its working for me This returns bitmap

bmp=getBitmapFromURL(ur url here);

write this method

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;
        }
明媚殇 2024-12-25 18:12:50

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

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;
}

As far as i have experienced the scenario like yours i found that the input stream for an image cannot be obtained by simple methods of getting stream try to update the following things in your code and then check results. I'm sure you will get what you want.

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

and here is the method which can be declared within class to be called directly in your decode stream method

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;
}
迷荒 2024-12-25 18:12:50

我的猜测是

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

My guess would be

URL url = new URL("some url path");
URLConnection urlConnection = url.openConnection();
BitmapDrawable image = new BitmapDrawable(urlConnection.getInputStream());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文