android 获取网络上服务器图片
16lz
2021-12-04
layout文件<?xml version="1.0" encoding="utf-8"?>
class
public class showpage extends AppCompatActivity { private String picture_String; private ImageView Image_license; private Bitmap img; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_showpage); Image_license = (ImageView) findViewById(R.id.Image_license); Intent intent = new Intent(); Bundle bundle = intent.getExtras(); picture_String = bundle.getString("license"); new DownloadImage().execute(); } private Bitmap bitmap; class DownloadImage extends AsyncTask { @Override protected Object doInBackground(String... string) { try { String PATH = "网络上照片的地址"; URL url = new URL(picture_String); // 打开一个连接 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setRequestMethod("GET"); if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { // 得到服务器返回过来的流对象 InputStream inputStream = httpURLConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(Object object) { super.onPostExecute(object); if (object != null) { bitmap = (Bitmap) object; new Handler().postDelayed(new Runnable() { @Override public void run() { Image_license.setImageBitmap(bitmap); } }, 2000); } else { //失败 Toast.makeText(showpage.this, "下载失败", Toast.LENGTH_LONG).show(); } } }}
更多相关文章
- 类和 Json对象
- android 指纹验证api
- android发送json并解析返回json
- android Dialog中SeekBar的使用方法
- Android中Parcelable接口用法
- Android(安卓)DOC文档分析——Dialog
- android 自定义TextView中Html超链接点击事件详解
- 初识android
- 【Android(安卓)内存优化】Bitmap 内存缓存 ( Bitmap 内存复用 |