Android displays network picture instances

  • 2020-06-12 10:32:37
  • OfStack

This article illustrates Android's method of displaying network pictures and sharing it for your reference. The specific methods are as follows:

In general, displaying a web image in Android is very simple. Here is a very simple example:

Step 1:

Create your Activity, in this case ViewWebImageActivity;
The code in ViewWebImageActivity is as follows:

String imageUrl = "https://www.ofstack.com/images/logo.gif"; // This is the web image you need to display --- I found it on the Internet 
Bitmap bmImg;
ImageView imView;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView = (ImageView) findViewById(R.id.imview);
imView.setImageBitmap(returnBitMap(imageUrl));
}
public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

Among them, returnBitMap(String url) method is the concrete realization of network image conversion into bitmap.

Step 2:

Modify your ES24en. xml file as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
< /LinearLayout>

Step 3:

Add to the node of your ES32en. xml file because Android has a lot of permissions, otherwise images will not be displayed on your emulator.

Hopefully, this article has helped you with your Android programming.


Related articles: