Reputation: 1016
I'm attempting to load images in to display next to text in a list view. Since scrolling is terrible without an asynctask, I'm attempting to implement that. Unfortunately, my code is giving me null pointer errors in onPostExecute() and I cannot seem to find out why. My code is as follows:
class LoadImage extends AsyncTask<Object, Void, Bitmap>{
private ImageView imv;
private Object imageViewHolder;
private String position;
private Object path;
public LoadImage(ImageView imv, String _position) {
Log.w("MyApp", "creating LoadImage");
this.imv = imv;
Log.w("MyApp", "got image view");
path = imv.getTag();
Log.w("MyApp", "Got Imageview Path");
position = _position;
Log.w("MyApp", "LoadImage created");
}
@Override
protected Bitmap doInBackground(Object... params) {
Log.w("MyApp", "in doInBackground");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Log.w("MyApp", "made bitmap factory");
Bitmap bm = null;
Log.w("MyApp", "Getting URL ID");
File dir = new File(PATH + position);
Log.w("MyApp", "Have URL ID");
if(dir.isDirectory()){
Log.w("MyApp","Dir is a directory");
File header = new File(dir.getAbsolutePath() + "/title");
Log.w("MyApp", "Checking for title");
if(header.isFile()){
bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options);
}
if(bm!=null){
Log.w("MyApp", "Title is good");
}else{
Context c = ReadingList.this;
bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery);
}
}else{
Log.w("MyApp", "Dir Not Directory");
Context c = ReadingList.this;
bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery);
}
return bm;
}
@Override
protected void onPostExecute(Bitmap result) {
if (!imv.getTag().equals(path)) {
Log.w("MyApp", "Not setting images");
return;
}
if(result != null && imv != null){
Log.w("MyApp", "setting bitmap");
imv.setImageBitmap(result);
}else{
Log.w("MyApp", "Nothing happened");
}
}
}
The problem I'm fighting with is not letting bitmaps be recycled along with the recycled view. I've looked at some examples on this site, and am not sure why this isn't working. A lot of code was borrowed from the response to this question. The onPostExecute doesn't input anything into the log, nor does it crash immediatly. It seems to take about two minutes to crash. The images are being pulled from the SD card, so I'm not sure why it's taking so long to decide if it wants to crash or not.
in doInBackground
made bitmap factory
Getting URL ID
Have URL ID
Dir is a directory
Checking for title
Title is good
in doInBackground
made bitmap factory
getting URL ID
Have URL ID
Dir is a directory
Checking title
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:305)
at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:1)
It goes on to repeat the first 7 lines multiple times after this, but the app has crashed and I cannot see if they do anything. onPostExecute does not ever seem to be called again however.
For reference:
304: if (!imv.getTag().equals(path)) {
305: Log.w("MyApp", "Not setting images");
306: return;
307: }
Upvotes: 1
Views: 309
Reputation: 1016
I found a solution that works for me. I used aQuery, and used the shouldDelay and async set image from file they provide.
if(header.isFile() && !aq.shouldDelay(position, convertView, parent,
header.getAbsolutePath())){
bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options);
showcase.setImageBitmap(bm);
} else {
showcase.setImageResource(R.drawable.missing_image);
}
Upvotes: 1