gpt4 book ai didi

android - 我打开失败 : EACCES (Permission denied) error when I try write data to SD card in Android 5. 1,为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:40 24 4
gpt4 key购买 nike

我需要保存一些数据到SD卡,我已经添加了权限 到AndroidManifest.xml文件,在Android 4.12 mobile上测试可以得到正确的结果。

但我在 Android 5.1 移动设备上测试它时出现 open failed: EACCES (Permission denied) 错误,为什么?

顺便说一句,我已经阅读了文章 Android 6.0 open failed: EACCES (Permission denied)Exception 'open failed: EACCES (Permission denied)' on Android , 但现在我的手机是三星 Android 5.1

代码

 private void ActionUploadFiles(Map<String, String> files,IHTTPSession session,String uploadFolder){

try{
Set<String> keys = files.keySet();

for (String key:keys) {
String location = files.get(key);
File source = new File(location);

String filename= session.getParms().get(key);
filename=FilenameUtils.getName(filename);

File target = new File(uploadFolder,filename);

FileUtils.copyFile(source,target);
}
}
catch (Exception e) {
Utility.LogError("Upload Error: "+ e.getMessage());
}
}

AndroidManifest.xml

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

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

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

<uses-permission android:name="com.android.vending.BILLING" />

build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
multiDexEnabled true

applicationId "info.dodata.wifi"
minSdkVersion 16
targetSdkVersion 23
versionCode 4
versionName "1.04"
archivesBaseName = "WiFiFileTransfer-V" + versionName
}

productFlavors {
free {
applicationId "info.dodata.wifi"
}

pro {
applicationId "info.dodata.wifi.pro"
}
}


buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "boolean", "IsDebugMode", "false"
}

debug {
buildConfigField "boolean", "IsDebugMode", "true"
}
}

}

最佳答案

您缺少从 Android Lollipop 引入的运行时权限。您可以做的是启用权限。为此,请转到

设置 -> 应用程序 -> YOUR_APP -> 权限

并启用权限,然后进行测试。

要添加运行时权限,您可以在onCreate()中询问。

String[] permission ={Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

if(ContextCompat.checkSelfPermission(EditDisplayPicture.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(EditDisplayPicture.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityCompat.requestPermissions(YourActivity.this, permission, 1);
}
} else proceedFurther();

然后像这样监听权限接受或拒绝,

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
proceedFurther();
} else {
Toast.makeText(YourActivity.this, "You need to give permission in order to proceed further.", Toast.LENGTH_SHORT).show();
}
}

关于android - 我打开失败 : EACCES (Permission denied) error when I try write data to SD card in Android 5. 1,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42621026/

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