gpt4 book ai didi

java - 检查服务器上是否存在图像文件(Android)

转载 作者:太空狗 更新时间:2023-10-29 16:37:59 25 4
gpt4 key购买 nike

我的服务器上有一个包含图像的文件目录。图像的名称与项目表相关联。项目 ID#1,将有一个对应的图像“1.png”。

并非所有项目都有图像,因此如果应用程序检查,它会在其中一些项目上找不到任何内容。在这种情况下,我需要一个 if 检查来显示默认图像。

我的问题:只有一个 URL 可用,我如何检查该 url 是否指向图像或者它是否指向任何内容/空白/null?

最佳答案

我建议请求图像请求的头部,因为它更快。

非毕加索:

HttpClient client= new DefaultHttpClient();
HttpHead headMethod = new HttpHead(urlToImage);
HttpResponse response = client.execute(headMethod);
if(response.getStatusLine().getStatusCode== HttpStatus.SC_NOT_FOUND) {
// Image does not exist, show a placeholder or something
} else {
// load image into the view.
}

Picasso :(与 head 版本无关,但这是处理它的简单替代方法,还可以查看 Picasso 可以接受的回调以微调成功/错误)

Picasso.with(context).load(urlToImage).into(imageView).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error);

引用资料:

Apache HttpHead from Android's SDK Reference

Apache HttpStatus from Android's SDK Reference

Square Picasso

关于java - 检查服务器上是否存在图像文件(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574892/

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