我有以字符串格式从图库中挑选的壁纸路径。 现在,从这条路径开始,我想从图库或本地存储中获取图像可绘制或位图,并将其设置为墙纸。
我所做的是:
if (!wallpaperPath.equals("") && wallpaperPath.length() > 0) {
Bitmap b = BitmapFactory.decodeFile(wallpaperPath);
WallpaperManager wallpaperManager = (WallpaperManager.getInstance(ScrollingActivity.this));
try {
wallpaperManager.setBitmap(b);
} catch (IOException e) {
e.printStackTrace();
}
但是这给了我错误
java.lang.RuntimeException: Error receiving broadcast Intent { act=WIFICHANGE flg=0x10 (has extras) } in delusions.com.myapplication.ScrollingActivity$2@4e59142
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:874)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at android.app.WallpaperManager.setWallpaper(WallpaperManager.java:1524)
at android.app.WallpaperManager.setBitmap(WallpaperManager.java:1278)
at android.app.WallpaperManager.setBitmap(WallpaperManager.java:1207)
at delusions.com.myapplication.ScrollingActivity$2.onReceive(ScrollingActivity.java:132)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:864)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
}
这样做的正确方法是什么?
获取壁纸的代码:
case R.id.btn_wallpaper:
Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
Intent chooser = Intent.createChooser(imageIntent,"Select New Wallpaper");
startActivityForResult(chooser,RESULT_WALLPAPER);
break;
和onActivityResult()
if(data!=null) {
Bundle bundle = data.getExtras();
System.out.println(" bundle string : "+ bundle.getString("data"));
System.out.println(" check here :"+ data.getExtras()!=null?"OK":"NOTOK");
System.out.println(" check here key 2:"+ data.getExtras().containsKey("dat")!=null?"OK":"NOTOK");
Bitmap imagedata = (Bitmap) data.getExtras().get("dat");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(imagedata!=null)
imagedata.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
tv_Ringtone.setText(encodedImage);
Prefs.writeString(this,Defines.IMAGE_DATA,encodedImage);
Uri wallpaperURI = data.getData();
String wallpaperPath = wallpaperURI.getPath();
System.out.println("Wallpaper path encoded: "+ wallpaperURI.getEncodedPath());
System.out.println("Wallpaper path : " + wallpaperPath);
Prefs.writeString(this, Defines.WALLPAPER_PATH, wallpaperPath);
iv_newWallpaper.setImageURI(wallpaperURI);
}
答案 0 :(得分:0)
我只从stackoverflow找到了我的问题的答案,但我错过了我所做的链接。
if(data!=null) {
Bundle bundle = data.getExtras();
final Uri wallpaperURI = data.getData();
new Thread(new Runnable() {
@Override
public void run() {
Bitmap imagedata = null;//(Bitmap) data.getExtras().get("dat");
try {
imagedata = MediaStore.Images.Media.getBitmap(getContentResolver(), wallpaperURI);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(imagedata!=null)
imagedata.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
Prefs.writeString(ScrollingActivity.this,Defines.IMAGE_DATA, encodedImage);
//tv_Ringtone.setText(encodedImage);
}
}).start();
String wallpaperPath = wallpaperURI.getPath();
System.out.println("Wallpaper path encoded: "+ wallpaperURI.getEncodedPath());
System.out.println("Wallpaper path : " + wallpaperPath);
Prefs.writeString(this, Defines.WALLPAPER_PATH, wallpaperPath);
iv_newWallpaper.setImageURI(wallpaperURI);
设置壁纸是
String previouslyEncodedImage = Prefs.readString(ScrollingActivity.this,Defines.IMAGE_DATA);
if( !previouslyEncodedImage.equalsIgnoreCase("") ){
byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
WallpaperManager wallpaperManager = (WallpaperManager.getInstance(ScrollingActivity.this));
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}