假设我正在尝试将位图图像保存为png
public void save() {
String filename = "file.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);
try {
FileOutputStream out = new FileOutputStream(dest);
fBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
如果我使用Android 6.0及更高版本,则需要请求运行时权限
void validatePermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
}
}
我有几个问题:
上面的代码成功请求权限,但是我必须重新启动该应用, 在授予或不授予权限之前,如何使代码暂停?
在Android 6.0以下版本中,清单文件会在安装时授予权限。 android 5或更低版本 处理运行时权限代码?
谢谢
答案 0 :(得分:0)
您不应该重新启动应用程序。您应该更改应用程序的逻辑:等待用户授予许可,然后再次尝试运行操作。 而且,是的,您可以为此使用第三方库。例如,我个人选择:权限调度程序。
答案 1 :(得分:0)
以上代码成功请求权限,但是我必须重新启动应用程序 在授予权限或不授予权限之前,我会逐渐暂停代码吗?
您可以使用此空白
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
}
// other 'case' lines to check for other
// permissions this app might request
}
}
在Android 6.0以下版本中,清单文件会在安装时授予权限。
android 5或更低版本的运行时权限代码?
代码将被跳过
答案 2 :(得分:0)
您可以使用 Dexter 库以获得更好的性能。我也在这里分享德克斯特代码
使用 Dexter,您需要在 app.build gradle 文件中添加 Dexterdependency
。
implementation 'com.karumi:dexter:6.2.2'
Single permission/////
Dexter.withContext(activity)
.withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(object : PermissionListener {
override fun onPermissionGranted(p0: PermissionGrantedResponse?) {
downloadImage(url)
}
override fun onPermissionDenied(p0: PermissionDeniedResponse?) {
if (p0!!.isPermanentlyDenied) {
showSettingsDialog()
}
}
override fun onPermissionRationaleShouldBeShown(
p0: PermissionRequest?, p1: PermissionToken?
) {
p1!!.continuePermissionRequest()
}
})
.onSameThread()
.check()
Multiple Permission////
Dexter.withContext(requireContext())
.withPermissions(Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(object : MultiplePermissionsListener {
override fun onPermissionsChecked(p0: MultiplePermissionsReport?) {
if (p0!!.areAllPermissionsGranted())
{
}else if (p0.isAnyPermissionPermanentlyDenied)
{
openSettings()
}
}
override fun onPermissionRationaleShouldBeShown(p0: MutableList<PermissionRequest>?, permissionToken: PermissionToken?) {
permissionToken?.continuePermissionRequest()
}
}).check()