open failed: EACCES (Permission denied) in Android(6.0.1) system application

907 Views Asked by At

I have developed a android system application to copy file from /sdcard/download/test.txt to /cache/xyz/ location.

I am able to copy the file to /cache/ , but bot into /cache/xyz/ location ,

Getting below error :

java.io.FileNotFoundException: /cache/xyz/test.txt: open failed: EACCES (Permission denied)

File packageFile = new File(Environment.getDownloadCacheDirectory() + "/xyz/test.txt");
                        File downloadedFile = new File(Environment.getExternalStorageDirectory() + "/test.txt");
                        if (packageFile.exists()) {
                            Log.d(TAG, "TEST -> File in Cache Exists");
                            
                        } else {
                            Log.d(TAG, "TEST -> File in Cache is Empty");
                        }
                        packageFile.canWrite();
                        if (downloadedFile.exists()) {
                            Log.d(TAG, "TEST -> packageFile in downloadedFile Exists");

                            FileChannel source = null;
                            FileChannel dest = null;

                            try {
                                source = (new FileInputStream(downloadedFile)).getChannel();
                                dest = (new FileOutputStream(packageFile)).getChannel();
        count += dest.transferFrom(source, count, size-count);

                               

                               
                            catch (Exception e) {
                                Log.d(TAG, "TEST -> Failed to copy update file into internal storage: " + e);
                            }

                           
                        } else {
                            Log.d(TAG, "TEST -> File DO NOT Exists");

                        }

Manifest :

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

There are 1 best solutions below

3
Son Tran On

For API 23+ you need to request the read/write permissions even if they are already in your manifest.

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

via: Exception 'open failed: EACCES (Permission denied)' on Android