Yudi Moszkowski Yudi Moszkowski - 6 months ago 43
Android Question

Display Image in Listview with AsyncTask

I am trying to display images in a listview with AsyncTask but I am getting an error. Saying...


Attempt to invoke virtual method 'java.lang.String
java.lang.Object.toString()' on a null object reference


This is error is happening on

public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}


And

LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();


Here is LoadImage.java

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

/**
* Created by Yudii on 27/05/2016.
*/
class LoadImage extends AsyncTask<Object, Void, Bitmap>{

private ImageView imv;
private String path;

public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}

@Override
protected Bitmap doInBackground(Object... params) {

Bitmap bitmap = null;
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + path);

if (file.exists()) {
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}

return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {


if (!imv.getTag().toString().equals(path)) {
/* The path is not same. This means that this
image view is handled by some other async task.
We don't do anything and return. */
// return;
}




if(result != null && imv != null){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}

}


And here is CustomList.java

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final Integer[] imageId;

public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.imageId = imageId;

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

LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);


LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();


imageView.setImageResource(imageId[position]);

return rowView;

}
}


And here is MainActivity.java

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

ListView list;
String[] web = {
"Google Plus",
"Twitter",
"Tall",
};

Integer[] imageId = {
R.drawable.gggggggggg,
R.drawable.hhhhhhhhhh,
R.drawable.ttttttttttt
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



CustomList adapter = new
CustomList(MainActivity.this, web, imageId);


list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

}
});

}
}

Answer

Why do you need AsyncTask? Try to use library Picasso and open images by one string:

File file = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + path);
Picasso.with(context).load(file).into(imageView);

Bitmap objects are very weight and you need to clean memory after each Bitmap image for prevent OutOfMemoryError. I recommend you to use libraries.

Comments