in manifest:
<uses-permission android:name="android.permission.INTERNET">
在 onCreate 方法中:
webImage = (ImageView) findViewById(R.id.webimage);
String urlImage = "https://thetab.com/blogs.dir/91/files/2017/01/maxresdefault-1.jpg";
// Set setImageBitmap to Bitmap created by getURLBitmap method
webImage.setImageBitmap(getURLBitmap(urlImage));
在 getURLBitmap 方法中:
if(!urlString.isEmpty() || urlString != null) {
InputStream inputStream = null;
// pass the string into a URL object
try {
URL urlForImage = new URL(urlString);
// cast url openConnection into HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) urlForImage.openConnection();
// Set HttpURLConnection setDoInput to true
connection.setDoInput(true);
// Start HttpURLConnection connection
connection.connect();
if(connection.getResponseCode() == 200) {
// Start reading Http inputStream (getInputStream) and use it to initialize a InputStream object
inputStream = connection.getInputStream();
// pass InputStream object into a BitmapFactory's decodeStream (is a static method)
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// set Bitmap object to decodedStream
return bitmap;
}
// return Bitmap
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
我不断收到此错误:
D/网络安全配置:未指定网络安全配置,使用平台默认值
E/Android运行时:致命异常:主进程:com.example.EX.perfectmoment,PID:26747java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.EX.perfectmoment/com.example.EX.perfectmoment.MemeMainActivity}: android.os.NetworkOnMainThreadException
您收到此错误是因为您在 UI 线程上运行网络操作,这在 Android Dev 中非常被看不起,因为它通常会导致 UI 无响应,从而导致糟糕的用户体验。我建议创建自己的ASyncTask,它将在另一个线程中执行网络操作并将其反馈到UI线程,或者使用Android上许多流行的图像库之一,例如Picasso或Glide。
如上面的评论中所述,不再支持在 android 中的 UI 线程上运行网络任务,因此您必须使用 AsyncTask 或其他可用的线程机制在单独的线程上执行 UI 阻止任务。
因此,通过使用AsynTask,您可以像下面的代码片段一样进行操作。
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity{
ImageView webImage;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webImage = (ImageView) findViewById(R.id.imageView1);
new SetImage().execute();
}
private class SetImage extends AsyncTask<Void, Void, Bitmap>{
final String urlImage = "https://thetab.com/blogs.dir/91/files/2017/01/maxresdefault-1.jpg";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap image = getURLBitmap(urlImage);
return image;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
webImage.setImageBitmap(bitmap);
}
}
private Bitmap getURLBitmap(String urlString){
if(!urlString.isEmpty() || urlString != null) {
InputStream inputStream = null;
// pass the string into a URL object
try {
URL urlForImage = new URL(urlString);
// cast url openConnection into HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) urlForImage.openConnection();
// Set HttpURLConnection setDoInput to true
connection.setDoInput(true);
// Start HttpURLConnection connection
connection.connect();
if(connection.getResponseCode() == 200) {
// Start reading Http inputStream (getInputStream) and use it to initialize a InputStream object
inputStream = connection.getInputStream();
// pass InputStream object into a BitmapFactory's decodeStream (is a static method)
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// set Bitmap object to decodedStream
return bitmap;
}
// return Bitmap
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}