I have below code 2 things , Volley
request that set an image , and array list adapter
that show static images.
I need help to add the vol
in other post you ask me for help. My image comes from a rest server as Json string Base64 coded. Then I do the next:
public View getView(int position, View convertView, ViewGroup parent) {
final ObjectLogados o = items.get(position);
final ViewHolder holder;
View vi = convertView;
if (vi == null){
LayoutInflater inflater = context.getLayoutInflater();
vi = inflater.inflate(R.layout.singlerowlog, null);// e' preciso fazer o inflate pois a view nao ainda nao esta em activity alguma
holder = new ViewHolder();
holder.ivImgMedLog= (ImageView) vi.findViewById(R.id.ivp_imagemedlog) ;
holder.tv_usuario = (TextView) vi.findViewById(R.id.tv_hnomemedlogado);
vi.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_usuario.setText(o.getUsuario());
String imageKey="MED"+String.valueOf(o.getNr_usuario());
final Bitmap bitmap = Common.getInstance().getBitmapFromMemCache(imageKey);
if (bitmap != null) {
holder.ivImgMedLog.setImageBitmap(bitmap);
}else {
holder.ivImgMedLog.setImageDrawable(context.getResources()
.getDrawable(R.drawable.empty));
downloadimage(holder.ivImgMedLog,o.getNr_usuario());
}
return vi;
}
static class ViewHolder {
TextView tv_usuario;
ImageView ivImgMedLog;
}
private void downloadimage(final ImageView imageView,final int nr){
//final ImageView imageView=imV;
String url=Common.getServerURL() + "\"RetrieveImageStaff\"";
JSONArray ja = new JSONArray();
ja.put(nr);
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("_parameters", ja);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url,
jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONObject anEntry = null, entries = null;
String ims = "";
try { // TODO Auto-generated method stub
JSONArray LResult = response.getJSONArray("result");
String s = LResult.get(0).toString();
anEntry = new JSONObject(s);
ims = anEntry.getString("imagem");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap decodedByte = null;
try {
if (!TextUtils.isEmpty(ims)) {
byte[] decodedString = Base64.decode(ims, Base64.DEFAULT);
decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if (imageView != null) {
if (decodedByte != null) {
imageView.setImageBitmap(decodedByte);
Common.getInstance().addBitmapToMemoryCache("MED"+String.valueOf(nr), decodedByte);
} else {
imageView.setImageDrawable(imageView.getContext().getResources()
.getDrawable(R.drawable.empty));
}
}
} else {
imageView.setImageDrawable(imageView.getContext().getResources()
.getDrawable(R.drawable.empty));
}
}catch (Exception e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
//Log.v(TAG, error.getMessage());
Toast.makeText(context, "Erro adquirindo imagem...." + error.getMessage(), Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
});
jr.setRetryPolicy(new DefaultRetryPolicy(Common.DEFAULT_TIMEOUT_MS,
Common.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Common.getInstance().addToReqQueue(jr, "jreq"); //reqQueue.add(jr);
}
You can change the request to use volley and get a image. So, you can change the code as described in https://developer.android.com/training/volley/request.html#request-image to something similar to:
ImageView mImageView;
String url = "http://i.imgur.com/7spzG.png";
mImageView = (ImageView) findViewById(R.id.myImage);
...
// Retrieves an image specified by the URL, displays it in the UI.
ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap);
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
mImageView.setImageResource(R.drawable.image_load_error);
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(request);
I think for your purpose you should use Picasso or Glide, instead of Volley.
Volley it's a amazing thing, but for list with image loading it's better to use more specific image loading library.
Here are links to Glide and Picasso tools: