To learn how to get image from url, we create a simple example.
Step 1: create new android project.
Step 2: file AndroidManifest.xml
add:
<uses-permission android:name="android.permission.INTERNET" />
Step 3: create the main layout: res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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.tutorialspots.getimage.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TUTORIALSPOTS.COM" android:textColor="#FF0000" android:textSize="30dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/textView" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> </RelativeLayout>
Step 4: create new class ImageViewHelper.java
package com.tutorialspots.getimage; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * Created by Tutorialspots on 1/10/2016. */ public class ImageViewHelper { public static Bitmap getBitmapFromURL(String src) { try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Sets the flag indicating whether this URLConnection allows input. connection.setDoInput(true); connection.connect(); InputStream inputStream = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } catch (Exception e) { e.printStackTrace(); return null; } } }
Step 5: file MainActivity.java
Method 1:
package com.tutorialspots.getimage; import android.graphics.Bitmap; import android.os.StrictMode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { String url = "http://tutorialspots.com/wp-content/uploads/2013/04/1.png"; ImageView imageView; Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); imageView = (ImageView) findViewById(R.id.imageView); bitmap = ImageViewHelper.getBitmapFromURL(url); if(bitmap == null) Toast.makeText(getApplicationContext(), "Can't get image from url", Toast.LENGTH_LONG).show(); else imageView.setImageBitmap(bitmap); } }
Note: if you don’t use the code:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
You will get the error:
01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: android.os.NetworkOnMainThreadException 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:214) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at com.tutorialspots.getimage.ImageViewHelper.getBitmapFromURL(ImageViewHelper.java:21) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at com.tutorialspots.getimage.MainActivity.onCreate(MainActivity.java:20) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at android.app.Activity.performCreate(Activity.java:5104) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-09 18:27:18.593 20909-20909/com.tutorialspots.getimage W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at android.app.ActivityThread.access$600(ActivityThread.java:141) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at android.os.Looper.loop(Looper.java:137) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5041) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at java.lang.reflect.Method.invoke(Method.java:511) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-09 18:27:18.597 20909-20909/com.tutorialspots.getimage W/System.err: at dalvik.system.NativeStart.main(Native Method)
Method 2: use AsyncTask
package com.tutorialspots.getimage; import android.graphics.Bitmap; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { String url = "http://tutorialspots.com/wp-content/uploads/2013/04/1.png"; ImageView imageView; Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); ImageLoadAsyncTask imageLoadAsyncTask = new ImageLoadAsyncTask(); imageLoadAsyncTask.execute(new String[]{url}); } public class ImageLoadAsyncTask extends AsyncTask<String, Void, String>{ @Override protected String doInBackground(String... strings) { bitmap = ImageViewHelper.getBitmapFromURL(strings[0]); return null; } @Override protected void onPostExecute(String s) { if(bitmap == null) Toast.makeText(getApplicationContext(), "Can't get image from url", Toast.LENGTH_LONG).show(); else imageView.setImageBitmap(bitmap); } } }
Step 6: run for test: