Reputation: 10199
I tried following the tutorial here: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html and I made the following BitmapWorkerTask.java file:
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
Log.d("NICK","doInBackground");
data = params[0];
return decodeSampledBitmapFromResource(getResources(), data, 100, 100);//**changed
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
Log.d("NICK","top of onPostExecute");
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
Log.d("NICK","imageView not null and bitmap not null");
if (imageView != null) {
Log.d("NICK","imageView not null");
imageView.setImageBitmap(bitmap);
}
}
Log.d("NICK","onPostExecute");
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
I had to change the line
return decodeSampledBitmapFromResource(getResources(), data, 100, 100);
to
return decodeSampledBitmapFromResource(System.getResources(), data, 100, 100);
because it was telling me cannot resolve method getResources().
When I try to actually use this my image view never gets an image set to it, it remains empty. My output log shows this when I open the activity with the ImageViews that need to be loaded: (loadBitmap is logged in my activity inside the loadBitmap function)
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.215: DEBUG/NICK(32327): loadBitmap
01-01 19:15:38.225: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.225: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.235: DEBUG/NICK(32327): doInBackground
01-01 19:15:38.355: INFO/System.out(32367): PackageUpdatedListener Data :: package:com.nick.simplequiz.paid
01-01 19:15:38.626: WARN/ActivityManager(653): Activity pause timeout for A ActivityRecord{656f0688 u0 com.nick.simplequiz.paid/.TabletGallery t556}
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): top of onPostExecute
01-01 19:15:39.066: DEBUG/NICK(32327): onPostExecute
Which is showing that the inside of the if statement in onPostExecute where the imageView is set to the image is never reached. I suspect this is what causes the imageview to never show an image but I don't know how to get this working properly. Any suggestions are greatly appreciated.
Upvotes: 0
Views: 777
Reputation: 6709
Try this, at the top of your main activity class, outside the onCreate method, define a variable of type Resources named res, then in your onCreate, say res = getResources();. Then in the decodeSampled method, use res. Try that. :)
Upvotes: 1