android – 如果没有从服务器返回图像,则在imageview中显示默认图像

目标

没有从服务器下载图像时显示默认图像.

问题

我有一个带有图像视图的列表视图(以及一些文本框,但这并不重要).
我的imageview为学生下载图像,但是当学生没有图像时,我试图显示默认图像.
我尝试了两件我认为应该工作的东西,设置了一个默认图像,或者下面的代码.
此代码取自活动文件,我将数据库列中的值写入变量(仅显示img以保持简单性)

                   //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");
                    }  

但是我在imgv =(ImageView)findViewById(R.id.ivImage)上获得了null引用;我不确定为什么我的图像视图被声明了.
没有任何帮助可以实现默认图像的效果
我们将不胜感激.

对于更多上下文,上面的代码是一个调用listview.xml的活动,然后调用row.xml.有问题的imageview在row.xml文件中.

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>

调用行的列表

<?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>

适配器:

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);


        }
    }
}
最佳答案
有一种更好的方法,你可以使用一个图像加载库,如:

Glide.

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

只需要一行即可完成你想要的任务.

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

它更容易,更清洁.

转载注明原文:android – 如果没有从服务器返回图像,则在imageview中显示默认图像 - 代码日志