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.