setting wallpaper through code

岁酱吖の 提交于 2019-12-09 07:12:29

问题


I was trying to make an app which also had the facility to let user select wallpaper he would like to set. I have managed this by calling the Gallery Intent. Once the user selects a specific image, the data path of the image is returned to me which then i preview to the user by setting the image onto an imageview.

The problem crops up when the image size (and/or resolution) is greater than what android expects. This results in failure of my module.

And as if this was not enough, wen the user tries to select some other wallpaper(and in my test case the "other" wallpaper was also of size >700kb) then the app crashes with the "OutOfMemoryException"...

Helppp me here guys!!!

For Gallery Intent i use:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

For setting the wallpaper i use:

InputStream is = getContentResolver().openInputStream(Uri.parse(uri_returned_from_intent)); 
Bitmap bgImage = BitmapFactory.decodeStream(is);//OutOfMemory error thrown here
setWallpaper(bgImage);

So i have 2 problems to deal with:

  1. How to crop the image before setting it as wallpaper...
  2. Cant understand y OutOfMemoryException is thrown, coz none of my image sizes exceed even 1mb... and i guess the VM budget in case Of N1 is 24Mb if m not mistaken...

回答1:


You should decode with inSampleSize option to reduce memory consumption.

  • Android: Strange out of memory issue while loading an image to a Bitmap object (stackoverflow)

Another option inJustDecodeBounds can help you to find correct inSampleSize value

  • How to get bitmap infomation before decode an image file? (Google Groups)


来源:https://stackoverflow.com/questions/3035050/setting-wallpaper-through-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!