我注意到我的新手机上的 Android 应用程序存在一个奇怪的问题。SDK 23 权限弹出窗口(如外部存储)被下面附加的警报阻止。我最初认为这与我的手机有关,但它似乎不会影响我安装的任何其他应用程序。
这个问题可能与安装调试版本有关,还是我的权限处理有问题?我认为它可能与我正在使用的广告平台之一有关,但我尝试禁用它们,但它仍然出现
我在下面粘贴了生成此权限请求的图像保存功能。我正在使用Dexter来节省编写一大堆可怕的样板文件
public static void saveToExternalStorageIfAllowed(final Context context, final Bitmap bitmapImage, final String title) {
final Tracker t = ((LoLHistory) context.getApplicationContext()).getTracker(LoLHistory.TrackerName.APP_TRACKER);
// saving to publicly visible/accessible folder. Requires write permission
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
// do not have permissions to write, request
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("PermissionMissing")
.setLabel("WRITE_EXTERNAL")
.build());
Dexter.checkPermission(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("PermissionGranted")
.setLabel("WRITE_EXTERNAL")
.build());
saveToExternalStorage(context, bitmapImage, title);
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("PermissionDenied")
.setLabel("WRITE_EXTERNAL")
.build());
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}, Manifest.permission.WRITE_EXTERNAL_STORAGE);
} else {
saveToExternalStorage(context, bitmapImage, title);
}
}
private static void saveToExternalStorage(Context context, Bitmap bitmapImage, String title) {
Tracker t = ((LoLHistory) context.getApplicationContext()).getTracker(LoLHistory.TrackerName.APP_TRACKER);
// create image folder if does not exist
File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), context.getString(R.string.app_name));
if (!imagesFolder.mkdirs() && !imagesFolder.isDirectory()) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// failed to create and is not a directory. Something went wrong...
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("CreateDirFailed")
.setLabel(imagesFolder.getPath())
.build());
} else {
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("CreateDirFailedMediaNotMounted")
.setLabel(imagesFolder.getPath())
.build());
}
}
// delete image if already exists so FOS can create a new one
File image = new File(imagesFolder, title + ".jpg");
if (image.exists()) {
// image already exists, deleting to start from clean state
if (!image.delete()) {
// failed to delete
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("DeleteFailed")
.setLabel(image.getPath())
.build());
}
}
// compress bitmap and write to file stream. FOS creates file if does not exist
FileOutputStream out = null;
try {
out = new FileOutputStream(image);
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(e.getLocalizedMessage())
.setFatal(true)
.build());
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(e.getLocalizedMessage())
.setFatal(true)
.build());
}
}
// get Uri from saved image
Uri uriSavedImage = Uri.fromFile(image);
// media scan the new file so it shows up in the gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uriSavedImage);
context.sendBroadcast(mediaScanIntent);
}
更新:由于很多人都提到它,如前所述,这个问题不是由于安装了覆盖应用程序。在Draw over other apps菜单下,我有以下应用程序:Google Play 音乐、Google Play 服务、照片、TalkBack、Twitch、Twitter。所有这些都设置为 No。
此外,我还测试了其他应用程序,例如 Google Hangouts 和 Twitter,它们也具有需要危险权限的操作,我能够提供这些权限而不会出现此问题。
解决方案:我已将R. Zagorski的答案标记为解决方案,因为它包含许多一般情况。对我来说,这实际上Toast
是破坏了我的权限流程。这个弹出窗口让我走错了路,浪费了很多时间......
这是Toast
我在权限弹出窗口出现后的最初几秒钟内可见的内容: