layout文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_showpage" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.yuyu.graduationadmin.Page.store.showpage"> <ImageView android:id="@+id/Image_license" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
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<String, Void, Object> {
@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();
}
}
}
}