本文介绍了撞车时,我使用的方法setWallpaper机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的照片与我的相机(手机),然后我需要将其设置墙纸,但让我崩溃。当我从 setWallpaper使用()它说我从类型上下文的方法setWallpaper(位图)是pcated德$ P $ 。这是我的code:

 公共类相机扩展了活动实施View.OnClickListener {
    的ImageButton IB;
    按钮B;
    ImageView的IV;
    我的意图;
    最终静态INT cameraData = 0;
    BMP位图;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.photo);
        初始化();
    }

    私人无效初始化(){
        IV =(ImageView的)findViewById(R.id.IVReturnedPic);
        IB =(的ImageButton)findViewById(R.id.IBTakePic);
        B =(按钮)findViewById(R.id.btnSetWall);
        b.setOnClickListener(本);
        ib.setOnClickListener(本);
    }

    @覆盖
    公共无效的onClick(视图v){
        开关(v.getId()){
        案例R.id.btnSetWall:
            尝试 {
                getApplicationContext()setWallpaper(BMP)。
            }赶上(IOException异常E){
                e.printStackTrace();
            }
            打破;
        案例R.id.IBTakePic:
            I =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
            startActivityForResult(ⅰ,cameraData);
            打破;
        }
    }

    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,因此code,数据);
        如果(结果code == RESULT_OK){
            捆绑额外= data.getExtras();
            BMP =(位图)extras.get(数据);
            iv.setImageBitmap(BMP);
        }
    }
}
 

从这里 setWallpaper()让我崩溃

  getApplicationContext()setWallpaper(BMP)。
 

注意:在此code我使用 View.OnClickListener

解决方案

试试这个办法,希望这将帮助你解决你的问题。

在声明类级别WallpaperManager对象:

 私人WallpaperManager wallpaperManager;
 

在初始化初始化WallpaperManager对象():

 私人无效初始化(){
  wallpaperManager = WallpaperManager.getInstance(本);
}
 

设置位图wallpaperManager对象。

 案例R.id.btnSetWall:
   尝试 {
       如果(BMP!= NULL){
           wallpaperManager.setBitmap(BMP);
       }其他{
           //在这里写你的位图NULL句柄code。
       }
   }赶上(IOException异常E){
     Log.e(TAG,不能设置图片作为壁纸,E);
   }
   打破;
 

添加此权限的Andr​​oidManifest.xml:

 <使用-权限的Andr​​oid:名称=android.permission.SET_WALLPAPER/>
 

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 setWallpaper(Bitmap) from the type Context is deprecated .Here is my code :

public class Camera extends Activity implements View.OnClickListener {
    ImageButton ib;
    Button b;
    ImageView iv;
    Intent i;
    final static int cameraData = 0;
    Bitmap bmp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);
        initialize();
    }

    private void initialize() {
        iv = (ImageView) findViewById(R.id.IVReturnedPic);
        ib = (ImageButton) findViewById(R.id.IBTakePic);
        b = (Button) findViewById(R.id.btnSetWall);
        b.setOnClickListener(this);
        ib.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnSetWall:
            try {
                getApplicationContext().setWallpaper(bmp);
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case R.id.IBTakePic:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameraData);
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            iv.setImageBitmap(bmp);
        }
    }
}

It get me crash from here setWallpaper():

getApplicationContext().setWallpaper(bmp);

Notice : In this code I am using View.OnClickListener .

解决方案

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" />

这篇关于撞车时,我使用的方法setWallpaper机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:53