maslan
maslan

Reputation: 2178

Android Storage permission WRITE ONLY?

I am really tired with debugging this problem. I have an application that writes to a storage. The application worked fine, until I got an update (I don't know which update caused it, because I had a break from Android development) - my Android version is 6.0.1 now.

Anyways I am trying to write to my external storage. I got prepared reading the new permission system for Android, got the permissions, and I am doing a check in my code:

checkSelfPermission("android.permission.READ_EXTERNAL_STORAGE")
checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE")

By debugging I've checked (I also get a code that checks and asks if permission is necessary, but I wanted to be sure that it really is ;) ) I've observed that they return value 0, which is equal to PackageManager.PERMISSION_GRANTED

So, the permissions are granted for sure. I am trying to create an App directory in the storage to write pdf files, store sqlite DB and stuff like that.

Here is how I create the directory:

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        File dir = Environment.getExternalStorageDirectory();
        String sNewDir = Constants.ApplicationDataDir;

        if (sSubDir == null) {
            sSubDir = "";
        } else {
            sSubDir = sSubDir.trim();
        }
        if (sSubDir.length() > 0) {
            sNewDir += File.separator + sSubDir;
        }
        File dirName = new File(dir, sNewDir);
        if (!dirName.exists()) {
            if (!dirName.mkdirs()) {
                LogDump.e("getAppDirectory err", " (sSubDir=<" + sSubDir + "> cannot create dir: " + dirName.getAbsolutePath());
            }
        }
        return dirName;
    } else {
        // Something else is wrong
        LogDump.e("getAppDirectory err", " (sSubDir=<" + sSubDir + "> not mounted state: " + state);
        return null;
    }

The value of dirName is /storage/emulated/0/MY_APP_CONST_VALUE/sSubDir (the sSubdir is a parameter for subdirectory ie. "Preferences") Soo, the code (checking with debugger) the execution checks that the directory does not exist, and the mkdirs() returns false result. THAT actually means that the directory does not exist, and could not be created.

During my application operation my logs get filled with:

 W/FileUtils: Failed to chmod(/storage/emulated/0/MY_APP_CONST_VALUE/DB/pcdrdata.sqlite3): android.system.ErrnoException: chmod failed: EPERM (Operation not permitted) 

from the SQLLite code And FileNotFound exception for any other file operations...

This seems quite logical since It cannot create the directories, right? Now for the most confusing part:

I am using total commander, and I can see that the directories are actually being created, the sqlite database is being copied, and all the files that do FileNotFoundException are there. I made a test, deleted them, rerun the App, and they are copied to the location once again. However any read operation on the files is not possible.

So it somehow creates the directories, allows to write (even thouh mkdirs() says otherwise) to them, but does not allow to read.

I am really confused, and tired of trying. I also tried the same without the SD card - identical results though.....

My application creates PDF files - they are created successful, but even the Total Commander says that it cannot open them....

My phone is Xperia Z3

Upvotes: 0

Views: 1015

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93614

As of KitKat you can't read from the root of the external storage anymore. You can only read in special public directories or in your own private directory on the sdcard. If you're seeing them created but not able to alter them, it seems like Android decided you could write there but not read there

Upvotes: 0

Related Questions