Reputation: 7377
I couldn't find any helpful answers on stackoverflow or google about displaying multiple images using Volley.
this is my code in displaying one image:
mainactivity.java
package com.example.zproject;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
public class MainActivity extends ActionBarActivity {
private NetworkImageView mNetworkImageView;
private ImageLoader mImageLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);
}
@Override
protected void onStart() {
super.onStart();
// Instantiate the RequestQueue.
mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
.getImageLoader();
//Image URL - This can point to any image file supported by Android
final String url = "http://goo.gl/0rkaBz";
mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
R.drawable.add, android.R.drawable.ic_dialog_alert));
mNetworkImageView.setImageUrl(url, mImageLoader);
}
}
the volley skeleton
package com.example.zproject;
import android.content.Context;
import android.graphics.Bitmap;
import android.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;
/**
* Custom implementation of Volley Request Queue
*/
public class CustomVolleyRequestQueue {
private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
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 CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
I had problem in this question because importing
import com.android.volley.toolbox.MultipartRequest;
import com.android.volley.examples.toolbox.MyVolley;
Upvotes: 0
Views: 1689
Reputation: 12293
You have to download images separetly, making separate request for each one.
You may find Picasso library very handy for image downloading and displaying
If you decide to use Volley
- there's also no problem to display several images at the same time.
If you have RecyclerView or some other view with a set of similar data then you have to use Adapter. Read this as an example. However you don't need this if you just have several ImageView
views on your layout
.
Upvotes: 1