This article describes the method of displaying network pictures on Android and is shared with you for your reference. The specific methods are as follows:
Generally speaking, it is actually very simple to display a network image in Android. Here is a very simple example:
Step 1:
① Create your Activity, in this example, use ViewWebImageActivity to describe it;
② The code in ViewWebImageActivity is as follows:
Copy the codeThe code is as follows:
String imageUrl = "https:///images/"; //This is the network image you need to display---you can find it casually online
Bitmap bmImg;
ImageView imView;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
imView = (ImageView) findViewById();
(returnBitMap(imageUrl));
}
public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
();
}
try {
HttpURLConnection conn = (HttpURLConnection) ();
(true);
();
InputStream is = ();
bitmap = (is);
();
} catch (IOException e) {
();
}
return bitmap;
}
Bitmap bmImg;
ImageView imView;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
imView = (ImageView) findViewById();
(returnBitMap(imageUrl));
}
public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
();
}
try {
HttpURLConnection conn = (HttpURLConnection) ();
(true);
();
InputStream is = ();
bitmap = (is);
();
} catch (IOException e) {
();
}
return bitmap;
}
③ Among them, the returnBitMap(String url) method is to specifically implement the conversion of network images into bitmap.
Step 2:
Modify your file as follows:
Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
< /LinearLayout>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
< /LinearLayout>
Step 3:
Add it on the node of your file, because Android has many permission restrictions, otherwise the image will not be displayed on your emulator.
I hope this article will be helpful to everyone's Android programming design.