Reputation: 9351
I have used toast in android to display messages many times and never had a problem, this includes putting it inside and outside of methods. however this time for some reason the compiler does not allow it to work. Why does it not allow toast to be put inside of this method shown below?
in this code I tried both types of context, "ThumbnailsActivity.class" and "this".
the method decodeSampleBitmapFromResource is inside the Android class ThumbnailsActivity.java that extends Activity. Nothing is unusual here.
public static Bitmap decodeSampledBitmapFromResource(String fileName, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(fileName, options);
// both of the toasts shown here have compile errors
Toast.makeText(ThumbnailsActivity.class, "TEST",Toast.LENGTH_LONG).show();
Toast.makeText(this, "TEST",Toast.LENGTH_LONG).show();
}//end decodeSampledBitmapfromresource method
Upvotes: 1
Views: 439
Reputation: 132982
Chanhe your method as:
public Bitmap decodeSampledBitmapFromResource(String fileName, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
// both of the toasts shown here have compile errors
Toast.makeText(ThumbnailsActivity.this, "TEST",Toast.LENGTH_LONG).show();
Toast.makeText(this, "TEST",Toast.LENGTH_LONG).show();
return BitmapFactory.decodeFile(fileName, options);
}//end decodeSampledBitmapfromresource method
put all toast before return statement and also remove static from method if you want to access non static context
Upvotes: 3
Reputation: 3796
U just write on getApplicationContext() and then check now,
Toast.makeText(getApplicationContext(),"TEST",Toast.LENGTH_LONG).show();
Upvotes: 0
Reputation: 37729
You cannot call current Activity
's Context
directly from a static
method.
You can pass current Activity
's Context
as param to static
method or make your method non-static.
Upvotes: 3