0
我想使用getCropAndSetWallpaperIntent來裁剪和保存圖像作爲牆紙。但我收到錯誤「Utils中的getImageUri(位圖,上下文)無法應用於(位圖)」。牆紙管理器,getCropAndSetWallpaperIntent錯誤
下面是代碼
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)
新的錯誤日誌
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;
}
你能爲我編輯一下嗎?我對android不友好。 – Ganesh
我認爲你不需要上面兩行。因爲你已經在函數參數中傳入了'bitmap'。所以,刪除這兩行並檢查。 –
我已經刪除它們,但「新意圖(wm.getCropAndSetWallpaperIntent(getImageUri(bitmap)));」仍然顯示錯誤(錯誤位於行尾的括號內的位圖上)。它要求我創建getImageUri方法,即使我擁有它。 – Ganesh