我正在尝试使用getCropAndSetWallpaperIntent进行裁剪并将图像保存为墙纸。但我收到错误“Utils中的getImageUri(位图,上下文)无法应用于(位图)”。
这是代码
public void setAsWallpaper(Bitmap bitmap) {
WallpaperManager imageView = null;
bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
WallpaperManager wm
= WallpaperManager.getInstance(_context);
try {
new Intent(wm.getCropAndSetWallpaperIntent(getImageUri(bitmap))); (**ERROR IS ON THIS LINE**)
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set_failed),
Toast.LENGTH_SHORT).show();
}
}
private Uri getImageUri(Bitmap inImage, Context inContext) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(),
inImage, "Title", null);
return Uri.parse(path);
}
这是logcat。
Process: com.example.wallpaper, PID: 25187
java.lang.NullPointerException: Attempt to invoke virtual method
'android.graphics.drawable.Drawable android.app.WallpaperManager.getDrawable()' on a null object reference
at com.example.wallpaper.util.Utils.setAsWallpaper(Utils.java:90)
at com.example.wallpaper.FullScreenViewActivity.onClick(FullScreenViewActivity.java:267)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
NEW ERROR LOG
E/MediaStore: Failed to insert image
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=11235, uid=10207 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1620)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1284)
at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:986)
at com.builditpossible.wallpaperpack.util.Utils.getImageUri(Utils.java:107)
at com.builditpossible.wallpaperpack.util.Utils.setAsWallpaper(Utils.java:93)
at com.builditpossible.wallpaperpack.FullScreenViewActivity.onClick(FullScreenViewActivity.java:259)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
设置壁纸和下载按钮
@Override
public void onClick(View v) {
Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
switch (v.getId()) {
// button Download Wallpaper tapped
case R.id.llDownloadWallpaper:
utils.saveImageToSDCard(bitmap);
break;
// button Set As Wallpaper tapped
case R.id.llSetWallpaper:
utils.setAsWallpaper(bitmap);
break;
default:
break;
}