Hello friends, welcome to our new Android App Tutorial. In this tutorial we will create a simple android volley example application to load images from internet. I have already posted some android volley example applications in previous tutorials. So in this tutorial we will use the volley library to load images from internet.
Creating Android Volley Example Project
- Open Android Studio and create a new project (I created LoadImageVolley)
- Now add volley library to your project. If you don’t know how to do this you can check my first android volley tutorial
- Now we need to create the layout for our main screen. We will create an EditText, a Button and an ImageView.
- This is the layout I created
- For creating the above layout you can use this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/editTextUrl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/buttonLoad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Image" /> </LinearLayout> <com.android.volley.toolbox.NetworkImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView" /> </LinearLayout> |
- As you can see in the above code we did not use the normal ImageView. We are using NetworkImageView.
- Now we will create a new java class for our CustomVolleyRequest. Create a new java class, I just created CustomVolleyRequest.java and Write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package net.simplifiedcoding.loadimagevolley; import android.content.Context; import android.graphics.Bitmap; import android.support.v4.util.LruCache; import com.android.volley.Cache; import com.android.volley.Network; import com.android.volley.RequestQueue; import com.android.volley.toolbox.BasicNetwork; import com.android.volley.toolbox.DiskBasedCache; import com.android.volley.toolbox.HurlStack; import com.android.volley.toolbox.ImageLoader; /** * Created by Belal on 10/8/2015. */ public class CustomVolleyRequest { private static CustomVolleyRequest customVolleyRequest; private static Context context; private RequestQueue requestQueue; private ImageLoader imageLoader; private CustomVolleyRequest(Context context) { this.context = context; this.requestQueue = getRequestQueue(); imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() { private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20); @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } }); } public static synchronized CustomVolleyRequest getInstance(Context context) { if (customVolleyRequest == null) { customVolleyRequest = new CustomVolleyRequest(context); } return customVolleyRequest; } public RequestQueue getRequestQueue() { if (requestQueue == null) { Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024); Network network = new BasicNetwork(new HurlStack()); requestQueue = new RequestQueue(cache, network); requestQueue.start(); } return requestQueue; } public ImageLoader getImageLoader() { return imageLoader; } } |
- We will use the above class for our volley request
- Now come to MainActivity.java and write the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package net.simplifiedcoding.loadimagevolley; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.NetworkImageView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText editTextUrl; private Button buttonLoad; private NetworkImageView imageView; private ImageLoader imageLoader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextUrl = (EditText) findViewById(R.id.editTextUrl); buttonLoad = (Button) findViewById(R.id.buttonLoad); imageView = (NetworkImageView) findViewById(R.id.imageView); buttonLoad.setOnClickListener(this); } private void loadImage(){ String url = editTextUrl.getText().toString().trim(); if(url.equals("")){ Toast.makeText(this,"Please enter a URL",Toast.LENGTH_LONG).show(); return; } imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext()) .getImageLoader(); imageLoader.get(url, ImageLoader.getImageListener(imageView, R.drawable.image, android.R.drawable .ic_dialog_alert)); imageView.setImageUrl(url, imageLoader); } @Override public void onClick(View v) { if(v == buttonLoad){ loadImage(); } } } |
- Now just add the internet permission to your manifest file
1 2 3 |
<uses-permission android:name="ANDROID.PERMISSION.INTERNET"/> |
- Now run your application, you will see the following output
Bingo! Its working absolutely fine. You can download the source code of my project from the link below.
[easy_media_download url=”https://adf.ly/1PYTwt” text=”Download Source”]
Android Volley Example to Load Image from Internet – Video Demo
So thats all for this Android Volley Example Application. Feel free to ask if having any query or doubt with your comments. Thank You 🙂