gpt4 book ai didi

android - 读取和写入外部存储权限不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:36 27 4
gpt4 key购买 nike

在我的应用程序中,我想获得用户权限以在外部存储上读取和写入数据,我已将权限标记放在我的 list 中,如下所示。但是当安装应用程序或运行时以及需要读取或写入存储的地方时,没有从用户那里获得许可并且读取和写入内存是行不通的,直到我手动转到设置应用程序访问并启用对内存的访问。

我的 list 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="javadi60.saber.storeassistant">

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

<application
android:allowBackup="true"
android:dialogTheme="@style/CustomDialog"
android:icon="@mipmap/myappicon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".insertData" />
<activity android:name=".setting" />
<activity android:name=".dellAll" />
<activity android:name=".reports" />
<activity android:name=".login" />
</application>

</manifest>

最佳答案

在 android API >= 23 中,您需要在运行时请求权限。看看here

像这样

        // Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}

然而,Ted permission library是避免此类样板代码的好库。

//call back after permission granted
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
}

@Override
public void onPermissionDenied(List<String> deniedPermissions) {
Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
}


};

//check all needed permissions together
TedPermission.with(this)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.check();

关于android - 读取和写入外部存储权限不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51374551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com