gpt4 book ai didi

Android使用json从服务器下载图像

转载 作者:搜寻专家 更新时间:2023-11-01 08:11:25 25 4
gpt4 key购买 nike

我的应用程序包含几个按钮,我想要的是通过单击任何按钮,它会将我定向到一个 URL,该 URL 又将我带到一个 json 对象页面,然后为我提供要显示的图像源我的安卓设备。

例如:按钮 1 -> http://a.b.c.d/loadview.htm?buttonid=B1 -> Json 对象(img src- 图像文件的 url)-> 显示在我的 android 设备上。

提前致谢:)

最佳答案

这是工作代码:从服务器下载图像并在 android 中显示

public void onCreate(Bundle savedInstanceState) 

{

Bitmap bitmap = DownloadImage("http://www.aaa.com/images//29_13.jpeg");

ImageView img = (ImageView) findViewById(R.id.imagefromserver);

img.setImageBitmap(bitmap);

}


private InputStream OpenHttpConnection(String urlString)
throws IOException

{

InputStream in = null;

int response = -1;

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}

关于Android使用json从服务器下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9176231/

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