Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display default image in imageview if no image returned from server

Tags:

android

Goal

Display a default image when no image is downloaded from server.

Problem

I have a listview with an imageview(along with a few textbox but thats not important). My imageview downloads images for students but when the student has no image I am trying to display a default image. I have tried two things I thought should work, set a default image, or the code below. This code is taken from an activity file where I write the values from the database columns to variables (only showed img to maintain simplicity)

                   //Image path returned
                    if (javaRealObject.getString("img").equals(""))
                    {
                        imgv = (ImageView)findViewById(R.id.ivImage);
                        imgv.setImageResource(R.drawable.emptyhead);

                        Log.d("Test", "Empty");
                    }
                    else//No image found in column
                    {
                        student.setImage(javaRealObject.getString("img"));
                        Log.d("Test","Not Empty");
                    }  

However I am getting a null refernce on imgv = (ImageView)findViewById(R.id.ivImage); and I am not sure why since my image view is being declared. Any help ti acheive the effect of a default image when none is supplied from the column will be appreciated.

For a bit more context, the code above is an activity that calls the listview.xml, which then calls the row.xml. the imageview in question is in row.xml file.

ROW.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:src="@drawable/empty_head" /> //default image here

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvFirstName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/primary"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>
    </LinearLayout>
</LinearLayout>

List that calls row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="450dp"
        tools:listitem="@layout/row" >
    </ListView>
</LinearLayout>

Adapter:

public class DriverAdapter extends ArrayAdapter<Drivers> {

    ArrayList<Drivers> ArrayListDrivers;
    int Resource;
    Context context;
    LayoutInflater vi;

    public DriverAdapter(Context context, int resource, ArrayList<Drivers> objects) {
        super(context, resource, objects);

        ArrayListDrivers = objects;
        Resource = resource;
        this.context = context;

        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = vi.inflate(Resource, null);
            holder = new ViewHolder();

            holder.imageview = (ImageView) convertView.findViewById(R.id.ivImage);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvFirstName);
            holder.tvDescription = (TextView) convertView.findViewById(R.id.tvLastName);
            holder.tvClientid = (TextView) convertView.findViewById(R.id.tvid);
            holder.tvExpires = (TextView) convertView.findViewById(R.id.tv_expdate);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.imageview.setImageResource(R.drawable.ic_launcher);
            new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage());


        Glide.with(holder.imageview.getContext())
                .load(new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage())        )
                .centerCrop()
                .placeholder(R.drawable.ic_launcher)
                .crossFade()
                .into(holder.imageview);


        holder.tvName.setText(ArrayListDrivers.get(position).getFirstname());
        holder.tvDescription.setText(ArrayListDrivers.get(position).getLastname());
        holder.tvClientid.setText(ArrayListDrivers.get(position).getClient_id());
        holder.tvExpires.setText("Expiry Date:"+ArrayListDrivers.get(position).getExpires());



        return convertView;


    }

    static class ViewHolder {
        public ImageView imageview;
        public TextView tvName;
        public TextView tvDescription;
        public TextView tvClientid;
        public TextView tvExpires;

    }


    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap cImg1 = null;


            try {

                byte[] decodedString = Base64.decode(urldisplay, Base64.DEFAULT);
                cImg1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);


            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return cImg1;


        }

        protected void onPostExecute(Bitmap result) {

            bmImage.setImageBitmap(result);


        }
    }
}
like image 979
Niana Avatar asked Oct 18 '15 05:10

Niana


People also ask

How do I change the default picture on Glide?

Add a template column in the Glide data editor, containing the image or url of the default picture. Then, add an if/then/else column in the data editor : if “user image” is empty then “default image” else “user image”.

Which attribute is used to set an image in ImageView * *?

src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.


1 Answers

There is a better way to do that, you can use one of the image loading libs like:

Glide.

An image loading and caching library for Android focused on smooth scrolling

and it will take you just one line to do what you want.

    Glide.with(myFragment)
    .load(url)
    .placeholder(R.drawable.loading_spinner)
    .into(myImageView);

it's easier and cleaner.

like image 176
humazed Avatar answered Oct 26 '22 02:10

humazed