在androidmanifest.xml中我有:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="23" />
在build.gradle中我有:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
multiDexEnabled true
applicationId "test"
minSdkVersion 16
targetSdkVersion 23
如果我尝试拨打CameraRoll.getPhotos()
来获取图片库照片,我会收到错误:
Could not get photos: need READ_EXTERNAL_STORAGE permission
at createErrorFromErrorData
我正在尝试使用Android 6和7在真实设备上运行应用程序,我试图让它像在IOS中一样工作 - 当调用getPhotos()来查看权限请求对话框时。
这有什么不对?
答案 0 :(得分:3)
使用react-native PermissionAndroid类。 https://facebook.github.io/react-native/docs/permissionsandroid
import { PermissionsAndroid } from 'react-native'
async requestExternalStoreageRead() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
{
'title': 'Cool App ...',
'message': 'App needs access to external storage'
}
);
return granted == PermissionsAndroid.RESULTS.GRANTED
}
catch (err) {
//Handle this error
return false;
}
}
getPhotos = async () => {
//Before calling getPhotos, request permission
if (await this.requestExternalStoreageRead()){
CameraRoll.getPhotos({
first: 1000,
assetType: 'All'
})
.then((r) => {
this.setState({ photos: r.edges, summary: `Number of photos found ${r.edges.length}` })
})
.catch((error) => {
this.setState({errorMsg: error.message});
})
}
}
答案 1 :(得分:2)
要请求任何权限,您可以使用以下代码并添加所需的权限。这是在访问与权限相关的任何数据
之前通过请求它们来处理运行时权限的方法public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
执行此操作后,对于API&gt; = 23的设备,您将在运行时弹出,然后在用户接受权限或拒绝权限后,将调用onRequestPermissionsResult
方法。所以在这里你必须处理你的检查是否用户授予了应用程序的权限。如果是,您可以继续使用您的逻辑
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0:
boolean isPerpermissionForAllGranted = false;
if (grantResults.length > 0 && permissions.length==grantResults.length) {
for (int i = 0; i < permissions.length; i++){
if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
isPerpermissionForAllGranted=true;
}else{
isPerpermissionForAllGranted=false;
}
}
Log.e("value", "Permission Granted");
} else {
isPerpermissionForAllGranted=true;
Log.e("value", "Permission Denied");
}
if(isPerpermissionForAllGranted){
// do your stuff here
}
break;
}
}
UPDATE 1 for react Native
String[] perms = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Checking permissions on init
checkPerms();
}
public void checkPerms() {
// Checking if device version > 22 and we need to use new permission model
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) {
// Checking if we can draw window overlay
if (!Settings.canDrawOverlays(this)) {
// Requesting permission for window overlay(needed for all react-native apps)
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}
for(String perm : perms){
// Checking each persmission and if denied then requesting permissions
if(checkSelfPermission(perm) == PackageManager.PERMISSION_DENIED){
requestPermissions(perms, PERMISSION_REQ_CODE);
break;
}
}
}
}
// Window overlay permission intent result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
checkPerms();
}
}
// Permission results
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case PERMISSION_REQ_CODE:
// example how to get result of permissions requests (there can be more then one permission dialog)
// boolean readAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
// boolean writeAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;
// checking permissions to prevent situation when user denied some permission
checkPerms();
break;
}
}
答案 2 :(得分:1)
Add This code to your Activity on create
if (ContextCompat.checkSelfPermission(UserActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(UserActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(UserActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
a);
}
}