public void loadPic(View view){
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("https://imgs.sunofbeaches.com/group1/M00/00/0A/rBsADV3XXsSAHK4hAALzq3aA-og709.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10000);
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
connection.setRequestProperty("Accept-Encoding","gzip,deflate");
connection.setRequestProperty("Accept","*/*");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode==HttpURLConnection.HTTP_OK) {
Log.d(TAG, "run: 请求成功");
InputStream inputStream = connection.getInputStream();
Log.d(TAG, "run: "+inputStream);
//转成Bitmap
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Log.d(TAG, "run:获取到图片 "+bitmap);
//更新UI在主线程里面
runOnUiThread(new Runnable() {
@Override
public void run() {
ImageView imageView = findViewById(R.id.result_image);
imageView.setImageBitmap(bitmap);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
2020-01-20 18:15:52.160 1383-1405/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 9531619 , only wrote 9531360
2020-01-20 18:15:52.165 10340-10347/com.example.androidnetwork I/art: Starting a blocking GC Instrumentation
2020-01-20 18:15:52.175 10340-10376/com.example.androidnetwork D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2020-01-20 18:15:52.341 10340-10376/com.example.androidnetwork D/PicLoadActivity: run: 请求成功
2020-01-20 18:15:52.341 10340-10376/com.example.androidnetwork D/PicLoadActivity: run: buffer(com.android.okhttp.internal.http.HttpConnection$FixedLengthSource@9952e32).inputStream()
2020-01-20 18:15:55.387 1383-1406/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 9838917 , only wrote 9686160
2020-01-20 18:16:49.460 10340-10347/com.example.androidnetwork I/art: Starting a blocking GC Instrumentation
2020-01-20 18:17:13.685 4404-4469/? W/PlayEventLogger: No account for auth token provided
您的每一个用心回答,都会让这个世界变得更美好一些!