我正在尝试制作一个墙纸应用程序,但我在通过单击按钮设置墙纸时遇到错误,但我得到了不推荐的错误
这是我的java代码'package com.example.neelaysrivastava.wallpaper;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Switch;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends Activity implements OnClickListener {
ImageView display;
int toPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
display = (ImageView) findViewById(R.id.imageView);
ImageView image1 = (ImageView) findViewById(R.id.iVmg1);
ImageView image2 = (ImageView) findViewById(R.id.iVmg2);
ImageView image3 = (ImageView) findViewById(R.id.iVmg3);
ImageView image4 = (ImageView) findViewById(R.id.iVmg4);
ImageView image5 = (ImageView) findViewById(R.id.iVmg5);
ImageView image6 = (ImageView) findViewById(R.id.iVmg6);
ImageView image7 = (ImageView) findViewById(R.id.iVmg7);
ImageView image8 = (ImageView) findViewById(R.id.iVmg8);
ImageView image9 = (ImageView) findViewById(R.id.iVmg9);
ImageView image10 = (ImageView) findViewById(R.id.iVmg10);
ImageView image11 = (ImageView) findViewById(R.id.iVmg11);
ImageView image12 = (ImageView) findViewById(R.id.iVmg12);
ImageView image13 = (ImageView) findViewById(R.id.iVmg13);
ImageView image14 = (ImageView) findViewById(R.id.iVmg14);
ImageView image15 = (ImageView) findViewById(R.id.iVmg15);
ImageView image16 = (ImageView) findViewById(R.id.iVmg16);
ImageView image17 = (ImageView) findViewById(R.id.iVmg17);
ImageView image18 = (ImageView) findViewById(R.id.iVmg18);
ImageView image19 = (ImageView) findViewById(R.id.iVmg19);
ImageView image20 = (ImageView) findViewById(R.id.iVmg20);
Button setWall =(Button)findViewById(R.id.button);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
image7.setOnClickListener(this);
image8.setOnClickListener(this);
image9.setOnClickListener(this);
image10.setOnClickListener(this);
image11.setOnClickListener(this);
image12.setOnClickListener(this);
image13.setOnClickListener(this);
image14.setOnClickListener(this);
image15.setOnClickListener(this);
image16.setOnClickListener(this);
image17.setOnClickListener(this);
image18.setOnClickListener(this);
image19.setOnClickListener(this);
image20.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.iVmg1:
display.setImageResource(R.drawable.img1);
break;
case R.id.iVmg2:
display.setImageResource(R.drawable.img2);
break;
case R.id.iVmg3:
display.setImageResource(R.drawable.img3);
break;
case R.id.iVmg4:
display.setImageResource(R.drawable.img4);
break;
case R.id.iVmg5:
display.setImageResource(R.drawable.img5);
break;
case R.id.iVmg6:
display.setImageResource(R.drawable.img6);
break;
case R.id.iVmg7:
display.setImageResource(R.drawable.img7);
break;
case R.id.iVmg8:
display.setImageResource(R.drawable.img8);
break;
case R.id.iVmg9:
display.setImageResource(R.drawable.img9);
break;
case R.id.iVmg10:
display.setImageResource(R.drawable.img10);
break;
case R.id.iVmg11:
display.setImageResource(R.drawable.img11);
break;
case R.id.iVmg12:
display.setImageResource(R.drawable.img12);
break;
case R.id.iVmg13:
display.setImageResource(R.drawable.img13);
break;
case R.id.iVmg14:
display.setImageResource(R.drawable.img14);
break;
case R.id.iVmg15:
display.setImageResource(R.drawable.img15);
break;
case R.id.iVmg16:
display.setImageResource(R.drawable.img16);
break;
case R.id.iVmg17:
display.setImageResource(R.drawable.img17);
break;
case R.id.iVmg18:
display.setImageResource(R.drawable.img1);
break;
case R.id.iVmg19:
display.setImageResource(R.drawable.img19);
break;
case R.id.iVmg20:
display.setImageResource(R.drawable.img20);
break;
case R.id.button:
InputStream yeaaa =getResources().openRawResource(toPhone);
Bitmap Whatever = BitmapFactory.decodeStream(yeaaa);
try{
getApplicationContext().setWallpaper(Whatever);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
`我使用的是API23,应用程序的最小API是16。那么我应该怎么做来修复这个错误
我试过墙纸管理器但是出错了
这是密码
case R.id.button:
InputStream yeaaa =getResources().openRawResource(toPhone);
Bitmap Whatever = BitmapFactory.decodeStream(yeaaa);
WallpaperManager myWallpaper =WallpaperManager.getInstance(this);
try{
myWallpaper.setBitmap(Whatever);
} catch (IOException e) {
e.printStackTrace();
}
break;
Logcat正在展示
03-02 06:37:44.374 9764-9764/?d/androidruntime:调用main entry com.android.commands.pm.pm
03-02 06:38:05.104 9777-9777/?d/androidruntime:checkjni关闭
03-02 06:38:05.104 9777-9777/?d/dalvikvm:正在尝试加载lib libjavacore.so 0x0
03-02 06:38:05.114 9777-9777/?d/dalvikvm:添加了共享库libjavacore.so 0x0
03-02 06:38:05.114 9777-9777/?d/dalvikvm:正在尝试加载lib libnativehelper.so 0x0
03-02 06:38:05.114 9777-9777/?d/dalvikvm:添加了共享库libnativehelper.so 0x0
03-02 06:38:05.114 9777-9777/?d/dalvikvm:在libnativehelper.so 0x0中找不到JNI加载,跳过init
03-02 06:38:05.124 9777-9777/?d/dalvikvm:注意:类landroid/app/activitymanagernative;有179个未实现(抽象)方法
03-02 06:38:05.214 9777-9777/?e/memtrack:无法加载memtrack模块(没有这样的文件或目录)
03-02 06:38:05.214 9777-9777/?e/android.os.debug:未能加载memtrack模块:-2
03-02 06:38:05.254 9777-9777/?d/androidruntime:调用主条目com.android.commands.pm.pm作为错误
最佳答案
你可以用
WallpaperManager.setBitMap()
因为setWallpaper()已被弃用。
如果有图像uri,请使用以下命令:
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
mImageView.setImageURI(imagepath)