gpt4 book ai didi

android - 设置 Uri 时出现位图错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:07 25 4
gpt4 key购买 nike

我想让 ImageView 在网站上显示图像。所以我创建了一个新的 ImageView 并执行 imgView.setImageURI(uri);

当我启动应用程序时,图像没有出现,并且出现错误“resolveUri failed on bad Bitmap Uri (uri)”。

关于如何解决这个问题有什么想法吗?

最佳答案

如果不是内容 URI,this link可能有帮助。这似乎表明 imgView.setImageURI() 不应用于常规 URI。复制相关位:

Yes, ImageView.setImageURI(ContentURI uri) works, but it is for content URIs particular to the Android platform, not URIs specifying Internet resources. The convention is applied to binary objects (images, for example) which cannot be exposed directly through a ContentProvider's Cursor methods. Instead, a String reference is used to reference a distinct content URI, which can be resolved by a separate query against the content provider. The setImageURI method is simply a wrapper to perform those steps for you.

I have tested this usage of setImageView, and it does work as expected. For your usage, though, I'd look at BitmapFactory.decodeStream() and URL.openStream().

同时为了使这个答案独立,来自该链接的另一篇文章的示例代码,展示了如何做到这一点:

private Bitmap getImageBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}

我没有测试过这段代码,我只是有点偏执,想确保 SO 答案有用,即使网络上的所有其他站点都消失了 :-)

关于android - 设置 Uri 时出现位图错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3681714/

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