I want to get the current image set as the wallpaper in Android device and set it to the image view in my list after scaling it . What i am doing id this:
final WallpaperManager wm = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wm.getDrawable();
Bitmap bitmap = drawableToBitmap(wallpaperDrawable);
imageview.setImageBitmap(bitmap);
The drawableToBitmap Function is as follows:
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
But i get this error :
01-29 13:30:50.150: E/dalvikvm-heap(3281): Out of memory on a 3891216-byte allocation.
01-29 13:30:50.160: I/dalvikvm(3281): "main" prio=5 tid=1 RUNNABLE
01-29 13:30:50.160: I/dalvikvm(3281): | group="main" sCount=0 dsCount=0 obj=0x409c0460 self=0x12810
01-29 13:30:50.170: I/dalvikvm(3281): | sysTid=3281 nice=0 sched=0/0 cgrp=default handle=1074082952
01-29 13:30:50.180: I/dalvikvm(3281): | schedstat=( 1725234737 955825527 142 ) utm=152 stm=20 core=0
01-29 13:30:50.190: I/dalvikvm(3281): at android.graphics.Bitmap.nativeCreate(Native Method)
01-29 13:30:50.190: I/dalvikvm(3281): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
01-29 13:30:50.190: I/dalvikvm(3281): at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
01-29 13:30:50.190: I/dalvikvm(3281): at android.app.WallpaperManager.generateBitmap(WallpaperManager.java:729)
01-29 13:30:50.190: I/dalvikvm(3281): at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:265)
01-29 13:30:50.200: I/dalvikvm(3281): at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:230)
01-29 13:30:50.200: I/dalvikvm(3281): at android.app.WallpaperManager.getDrawable(WallpaperManager.java:350)
01-29 13:30:50.210: I/dalvikvm(3281): at storeapps.com.profilechanger.ListActivity.onCreate(ListActivity.java:64)
01-29 13:30:50.210: I/dalvikvm(3281): at android.app.Activity.performCreate(Activity.java:4466)
01-29 13:30:50.220: I/dalvikvm(3281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-29 13:30:50.220: I/dalvikvm(3281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-29 13:30:50.220: I/dalvikvm(3281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-29 13:30:50.220: I/dalvikvm(3281): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-29 13:30:50.220: I/dalvikvm(3281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-29 13:30:50.220: I/dalvikvm(3281): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 13:30:50.220: I/dalvikvm(3281): at android.os.Looper.loop(Looper.java:137)
01-29 13:30:50.230: I/dalvikvm(3281): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-29 13:30:50.230: I/dalvikvm(3281): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 13:30:50.230: I/dalvikvm(3281): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 13:30:50.230: I/dalvikvm(3281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-29 13:30:50.230: I/dalvikvm(3281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-29 13:30:50.230: I/dalvikvm(3281): at dalvik.system.NativeStart.main(Native Method)
01-29 13:30:50.270: I/Process(570): Sending signal. PID: 3281 SIG: 3
01-29 13:30:50.270: I/dalvikvm(3281): threadid=3: reacting to signal 3
01-29 13:30:50.310: W/WallpaperManager(3281): Can't generate default bitmap
01-29 13:30:50.310: W/WallpaperManager(3281): java.lang.OutOfMemoryError
01-29 13:30:50.310: W/WallpaperManager(3281): at android.graphics.Bitmap.nativeCreate(Native Method)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.WallpaperManager.generateBitmap(WallpaperManager.java:729)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:265)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:230)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.WallpaperManager.getDrawable(WallpaperManager.java:350)
01-29 13:30:50.310: W/WallpaperManager(3281): at storeapps.com.profilechanger.ListActivity.onCreate(ListActivity.java:64)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.Activity.performCreate(Activity.java:4466)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.os.Looper.loop(Looper.java:137)
01-29 13:30:50.310: W/WallpaperManager(3281): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-29 13:30:50.310: W/WallpaperManager(3281): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 13:30:50.310: W/WallpaperManager(3281): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 13:30:50.310: W/WallpaperManager(3281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-29 13:30:50.310: W/WallpaperManager(3281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-29 13:30:50.310: W/WallpaperManager(3281): at dalvik.system.NativeStart.main(Native Method)
01-29 13:30:50.310: D/ProfileChangerApp(3281): wallpaper manager is :android.app.WallpaperManager@41085b88
01-29 13:30:50.320: D/ProfileChangerApp(3281): image is not null
01-29 13:30:50.320: I/dalvikvm(3281): Wrote stack traces to '/data/anr/traces.txt'
01-29 13:30:50.340: D/AndroidRuntime(3281): Shutting down VM
01-29 13:30:50.371: W/dalvikvm(3281): threadid=1: thread exiting with uncaught exception (group=0x409bf1f8)
01-29 13:30:50.430: E/AndroidRuntime(3281): FATAL EXCEPTION: main
01-29 13:30:50.430: E/AndroidRuntime(3281): java.lang.RuntimeException: Unable to start activity ComponentInfo{storeapps.com.profilechanger/storeapps.com.profilechanger.ListActivity}: java.lang.NullPointerException
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.os.Looper.loop(Looper.java:137)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-29 13:30:50.430: E/AndroidRuntime(3281): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 13:30:50.430: E/AndroidRuntime(3281): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 13:30:50.430: E/AndroidRuntime(3281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-29 13:30:50.430: E/AndroidRuntime(3281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-29 13:30:50.430: E/AndroidRuntime(3281): at dalvik.system.NativeStart.main(Native Method)
01-29 13:30:50.430: E/AndroidRuntime(3281): Caused by: java.lang.NullPointerException
01-29 13:30:50.430: E/AndroidRuntime(3281): at storeapps.com.profilechanger.ListActivity.onCreate(ListActivity.java:70)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.Activity.performCreate(Activity.java:4466)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-29 13:30:50.430: E/AndroidRuntime(3281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-29 13:30:50.430: E/AndroidRuntime(3281): ... 11 more
Please help me !
Resize your image Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.yourImageName);
Above line will get Bitmap
from Drawable
. No need to do anything apart from adding the above line to get a Bitmap
from a Drawable
.
int height = (bitmap.getHeight() * 512 / bitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(bitmap, 512, height, true);
imageView.setImageBitmap(scale);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With