gpt4 book ai didi

android - 从 png.class 读取错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:54 28 4
gpt4 key购买 nike

当我尝试从 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。

最佳答案

我希望这就足够了。

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

关于android - 从 png.class 读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8315105/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com