Display default image in image view if image is not returned from server

goal

Display the default image when the image does not load from the server.

Problem

I have a listview with an image view (along with several text fields, but that doesn't matter). My image is viewing images for students, but when the student has no image, I try to display the default image. I tried two things that I thought should work, set the default image or the code below. This code is taken from an activity file where I write values ​​from database columns to variables (only to show img 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 get a null refernce on imgv = (ImageView)findViewById(R.id.ivImage); , and I'm not sure why since the announcement of my image. Any help can affect the default image, when none of them is delivered from the column will be appreciated.

For a bit more context, the above code is an action that calls listview.xml, which then calls row.xml. the image in question is in the 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 calling string

 <?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); } } } 
+5
source share
5 answers

There is a better way to do this, you can use one of the downloadable images, for example:

Glide .

Android Image Download and Caching Library focuses on smooth scrolling

and you only need one line to do what you want.

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

it's easier and cleaner.

+13
source

The Listview adapter is responsible for managing the presentation of your strings. What does your adapter look like? (the object you are calling listview.setAdapter (adapter)).

Inside the adapter there is a getView method that you need to override and return a view object. You need to call view.findviewbyId () on this view object, not your activity.

UPDATE Try deleting these lines

 holder.imageview.setImageResource(R.drawable.ic_launcher); new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage()); 

P / s can you explain the purpose of calling Glide.with (Context) .load (object)? thanks

+2
source

This method will work - Set the default image in your xml code, which will be displayed if the student does not have an image. Dynamically adjust the image using setImageResource (...) if you have an image.

Recheck the identifier of your ImageView when you receive a null-link error, and if this does not solve the problem, send the full operation code.

+1
source

In the AsyncTask constructor try to initialize the look of your image as follows

 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View convertView = inflater.inflate(R.layout.row, null, false); ivImage= (ImageView ) convertView.findViewById(R.id.ivImage); 

here row.xml where you declare ImageView ... and make sure ivImage is a global variable ... now ivImage is not null. I checked it .. but it is better to use the adapter class to control the listview row....

+1
source

I solved this by making something very simple. In the post exeucte of the aync method, I just said.

  if (result != null){ bmImage.setImageBitmap(result); } 

Thus, if this value is null, the default image will not be written using "null", Thanks for the manual

+1
source

Source: https://habr.com/ru/post/1233947/


All Articles