Reputation: 23
EDIT: I have the "Method call..." error fixed. Now my main issue is actually loading in the bitmap from my URL. I'm apparently not handling it properly and I'm getting java.lang.OutOfMemoryError
when I try to use the .get()
method from PIcasso.
What is the best way to do this? I don't even need the whole image, just a center crop to fill the wallpaper on my device. But I can't crop it without loading it in first, which is my issue.
So I'm trying to get a bitmap from a given URL using Picasso, then paint text on top of it and set it as my wallpaper background. I'm currently attempting to do this from onPostExecute in AsyncTask. The error I'm getting is:
java.lang.IllegalStateException: Method call should not happen from the main thread.
The line of code causing the problem is:
Bitmap bitmap = Picasso.with(myContext)
.load(url + ".jpg")
.fit()
.centerCrop()
.get();
and the whole onPostExcute function:
protected void onPostExecute(String url) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.preview);
try {
Bitmap bitmap = Picasso.with(myContext)
.load(url + ".jpg")
.fit()
.centerCrop().get();
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.RED); //Text Color
paint.setStrokeWidth(72); //Text Size
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); //Text Overlapping Pattern
canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.drawText("Test...", 10, 10, paint);
((ImageView) rootView.findViewById(R.id.preview)).setImageBitmap(bitmap);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(myContext);
//wallpaperManager.suggestDesiredDimensions(width, height);
wallpaperManager.setBitmap(bitmap);
Toast.makeText(myContext, "Wallpaper set successfully.", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
android.util.Log.e(TAG, "Failed to set wallpaper", e);
}
}
I haven't even been able to test all the canvas/paint code to see if it works. If you see anything wrong in there as well please let me know. Any and all tips are greatly appreciated. Thanks.
Upvotes: 0
Views: 1879
Reputation: 3045
Just putting both @jayeshsolanki93 and @zapl comments together. You are getting that error because you are calling the method get()
from Picasso
on the main thread. get()
is a synchronous method and hence the error. You have two options:
get()
to doInBackground
method of your AsyncTask
and then do all your canvas operations on onPostExecute
.Take full advantage of Picasso and use the Asynchronous methods that already has and do something like this:
Picasso.with(context).load(url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i(TAG, "The image was obtained correctly, now you can do your canvas operation!");
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e(TAG, "The image was not obtained");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.(TAG, "Getting ready to get the image");
//Here you should place a loading gif in the ImageView to
//while image is being obtained.
}
});
Upvotes: 1