我有壁纸的应用程序,我允许用户直接从应用程序设置壁纸。在这里,我将图像的http url转换为Bitmap并调用Android的WallpaperManager
类。
以下是将网址转换为位图并设置为WallpaperManager
位图的代码:
private void setWallpaper() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
Bitmap image = getBitMapFromHttpURL();
if (image != null) {
wallpaperManager.setBitmap(getBitMapFromHttpURL());
image.recycle();
image = null;
} else {
Toast.makeText(this, "Failed to set wallpaper",
Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
} catch (Exception es) {
}
}
此行wallpaperManager.setBitmap(getBitMapFromHttpURL());
失败,但出现以下异常:
java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=21407, uid=10133 requires android.permission.WRITE_SETTINGS, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1465)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:468)
at android.content.ContentResolver.insert(ContentResolver.java:1190)
at android.provider.Settings$NameValueTable.putString(Settings.java:843)
at android.provider.Settings.putString(Settings.java:6792)
at android.provider.Settings.putString(Settings.java:6717)
at android.app.WallpaperManager.willSendBroadcat(WallpaperManager.java:1124)
at android.app.WallpaperManager.setBitmap(WallpaperManager.java:788)
at com.wallie.WallpaperFullScreenActivity.setWallpaper(WallpaperFullScreenActivity.java:453)
at com.wallie.WallpaperFullScreenActivity.access$1(WallpaperFullScreenActivity.java:448)
at com.wallie.WallpaperFullScreenActivity$WallpaperManagerTask.doInBackground(WallpaperFullScreenActivity.java:473)
at com.wallie.WallpaperFullScreenActivity$WallpaperManagerTask.doInBackground(WallpaperFullScreenActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:238)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
可能是什么原因?
答案 0 :(得分:5)
您获得的异常明确定义您需要在Manifest中添加权限。
java.lang.SecurityException:Permission Denial:编写com.android.providers.settings.SettingsProvider uri content:// settings / system from pid = 21407,uid = 10133需要android.permission.WRITE_SETTINGS或grantUriPermission()< / p>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
答案 1 :(得分:2)
logcat很清楚:
java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=21407, uid=10133 requires android.permission.WRITE_SETTINGS, or grantUriPermission()
所以你只需要在你的清单文件中添加这个权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
在申请标签下。
希望它有所帮助。
答案 2 :(得分:1)
您缺少在AndroidManifest.xml中添加WRITE_SETTINGS权限。
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
如果你不需要更长的Bitmap对象,那么使用getBitMapFromHttpURL直接从bitamp检查,而不是声明Bitamp对象。
if (getBitMapFromHttpURL() != null) {
wallpaperManager.setBitmap(getBitMapFromHttpURL());
} else {
Toast.makeText(this, "Failed to set wallpaper",Toast.LENGTH_LONG).show();
}
答案 3 :(得分:0)
你可能忘了带任何这些权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />