Crash when I use method setWallpaper android?

前端 未结 4 1965
野的像风
野的像风 2021-01-27 18:23

I am getting photo with my camera(mobile) and then I need that to set it wallpaper but get me crash. When I use from setWallpaper() it say me The method setW

相关标签:
4条回答
  • 2021-01-27 18:39

    You need to use the WallpaperManager Class if You develop higher API Level 5:

    WallpaperManager mManager = WallpaperManager.getInstance(getApplicationContext());
    
      try {
          mManager.setResource(R.drawable.yourDrawable);
      } catch (IOException e) {
    
       //warning for the user
    e.printStackTrace();
    
       }
    

    And to use the manager, You need to set permissions SET_WALLPAPER in the manifest. Also, if You develop under API Level 5 and You want to use the method that You used, You have to set the Permission too.

    0 讨论(0)
  • 2021-01-27 18:40

    Have you added the wallpaper permission to your manifest?

     <uses-permission android:name="android.permission.SET_WALLPAPER" />
    
    0 讨论(0)
  • 2021-01-27 18:42

    I got the same problems in my android apps while setting a textarea text..... but i was able to fixed it by having an object reference in OnCreate method!!!!!! and then by using that global reference i was able to set the TextArea text property you have to look similar kind of solution...

    0 讨论(0)
  • 2021-01-27 18:45

    Try this way,hope this will help you to solve your problem.

    Declare WallpaperManager object at class level :

    private WallpaperManager wallpaperManager;
    

    Initialize WallpaperManager object in initialize() :

    private void initialize() {
      wallpaperManager = WallpaperManager.getInstance(this);
    }
    

    Set bitmap to wallpaperManager object.

    case R.id.btnSetWall:
       try {
           if(bmp!=null){
               wallpaperManager.setBitmap(bmp);
           }else{ 
               // write your bitmap null handle code here.
           }
       } catch (IOException e) {
         Log.e(TAG, "Cannot set image as wallpaper", e);
       }
       break;
    

    Add this permission to AndroidManifest.xml :

    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    
    0 讨论(0)
提交回复
热议问题