获取当前壁纸安卓13,更详细的问题



异常发生在:

final Drawable wallpaperDrawable = wallpaperManager.getDrawable();

"Permissionandroid.Permission.READ_EEXTERNAL_STORAGE被拒绝";这可能意味着wallpaperManager使用READ_EXTERNAL_STORAGE权限来工作,并且需要找到其他方法来使用READ_MEDIA_IMAGES权限来获得它

添加了这个问题(尽管它是第二个(,因为它包含了更多可能有助于解决这个问题的代码和规范

清单:

<uses-permission android:name="android.permission.READ_STORAGE_PERMISSION" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

附加的主代码,试图更改我的方法以包括android 13,目前我看到了权限窗口,并在那之后卡住了:

public void save_current_wallpaper_to_internal_storage() {
//get-current-wallpaper
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
//READ_MEDIA_IMAGES for android 13
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_MEDIA_IMAGES}, READ_REQUEST_CONST);
} else {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bitmap = drawableToBitmap(wallpaperDrawable);
saveToInternalStorage(bitmap);
}
} else {
//READ_EXTERNAL_STORAGE android < 13
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_REQUEST_CONST);
} else {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bitmap = drawableToBitmap(wallpaperDrawable);
saveToInternalStorage(bitmap);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == READ_REQUEST_CONST) {// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
final Drawable wallpaperDrawable = wm.getDrawable();
Bitmap bitmap = drawableToBitmap(wallpaperDrawable);
saveToInternalStorage(bitmap);
}
}
else{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
final Drawable wallpaperDrawable = wm.getDrawable();
Bitmap bitmap = drawableToBitmap(wallpaperDrawable);
saveToInternalStorage(bitmap);
}
}
final Drawable wallpaperDrawable = wm.getDrawable();
Bitmap bitmap = drawableToBitmap(wallpaperDrawable);
saveToInternalStorage(bitmap);
} else {
Log.v("TAG", "denied");
}
}
}

错误:

FATAL EXCEPTION: Thread-5
Process: com.DailyWallpaper, PID: 9186
java.lang.SecurityException: Permission android.permission.READ_EXTERNAL_STORAGE denied 
for package com.DailyWallpaper
at android.os.Parcel.createExceptionOrNull(Parcel.java:3011)
at android.os.Parcel.createException(Parcel.java:2995)
at android.os.Parcel.readException(Parcel.java:2978)
at android.os.Parcel.readException(Parcel.java:2920)
at 
android.app.IWallpaperManager$Stub$Proxy.getWallpaperWithFeature(IWallpaperManager.java:895)
at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:667)
at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:563)
at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:538)
at android.app.WallpaperManager.getDrawable(WallpaperManager.java:791)
at com.Daily_Wallpaper.MainActivity.save_current_wallpaper_to_internal_storage(MainActivity.java:127)
at com.Daily_Wallpaper.MainActivity$$ExternalSyntheticLambda12.run(Unknown Source:2)
at java.lang.Thread.run(Thread.java:1012)
Caused by: android.os.RemoteException: Remote stack trace:
at android.os.storage.StorageManager.checkPermissionAndAppOp(StorageManager.java:1805)
at android.os.storage.StorageManager.checkPermissionAndAppOp(StorageManager.java:1782)
at android.os.storage.StorageManager.checkPermissionAndAppOp(StorageManager.java:1849)
at android.os.storage.StorageManager.checkExternalStoragePermissionAndAppOp(StorageManager.java:1965)
at android.os.storage.StorageManager.checkPermissionReadImages(StorageManager.java:1931)

这是一个已知的问题。看见https://issuetracker.google.com/issues/237124750和https://issuetracker.google.com/issues/236690156

到目前为止还没有可接受的解决方案。

相关内容

  • 没有找到相关文章

最新更新