Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get open failed: EACCES (Permission denied) error when I try write data to SD card in Android 5.1, why?

Tags:

android

I need to save some data to SD card , I have add the permission to AndroidManifest.xml file, and I can get the correct result when I test it in Android 4.12 mobile.

But I get open failed: EACCES (Permission denied) error when I test it in Android 5.1 mobile, why?

BTW, I have read the artical Android 6.0 open failed: EACCES (Permission denied) and Exception 'open failed: EACCES (Permission denied)' on Android , but now my mobile is SamSung Android 5.1

Code

 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"
        }
    }

}
like image 426
HelloCW Avatar asked Mar 06 '17 08:03

HelloCW


1 Answers

You are missing the run-time permissions introduced from Android Lollipop. What you can do is enable permission. To do this, go to

Settings -> Apps -> YOUR_APP -> Permissions

and enable permissions and then test it.

For adding run-time permission, you can ask inside 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();

And then listen for permission acceptance or denial like this,

@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();
    }
}
like image 129
Waqas Ahmed Ansari Avatar answered Oct 25 '22 17:10

Waqas Ahmed Ansari