Search code examples
androidfile-permissionsfileoutputstream

FileNotFoundException:EACCES (Permission denied)


I get data from my service :

        JSONObject userGuid = new JSONObject();
        userGuid.put("userGuid", id);
        Bitmap bitmap = null;
        String response = HttpUtil.post(mService + "GetUser", userGuid.toString(), mCookie);

        JSONObject result = new JSONObject(response).getJSONObject("User");
        String temp = result.getJSONArray("UserImage").toString();

in received data, there is an Base64 image user and I got it and convert it to arryByte and then I convert it to InputStream :

        byte[] tmp = Base64.decode(temp, Base64.DEFAULT);
        InputStream is = new ByteArrayInputStream(tmp);

I want to write it to a File by outputStream :

OutputStream os = new FileOutputStream(f);

but I got this error:

java.io.FileNotFoundException: /storage/emulated/0/fcImages/581864034: open failed: EACCES (Permission denied)

In manifest I added these permissions:

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

What is your Idea?

Edit I create file in this way:

public class FileCache {

private File cacheDir;

public FileCache(Context context) {

    if((Environment
            .getExternalStorageState()).equals(Environment.MEDIA_MOUNTED)) {
            cacheDir = new File(Environment.getExternalStorageDirectory(), "fcImages");
    } else {
        cacheDir = context.getCacheDir();
    }
    if(!cacheDir.mkdirs()) {
        cacheDir.mkdirs();
    }
}

public File getFile(String id) {
    String filename = String.valueOf(id.hashCode());
    File f = new File(cacheDir, filename);
    return f;
}

Solution

  • There are 3 things that come in my mind to check:

    1. uses-permission tag is inside manifest tag (and not application tag)
    2. EACCESS may due to the fact that fcImages directory does not exist, or it is a file
    3. permission for Android api 23 must be requested runtime

    Source for (1) is this answer, while source for (3) is this answer