0

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" />
GNK
  • 1,036
  • 2
  • 10
  • 29
  • Try File.exists() and File.canWrite() on the directory before trying to put a file in it. – blackapps Jun 07 '21 at 07:08
  • @blackapps, no help – GNK Jun 07 '21 at 07:34
  • What do you mean? Why dont you tell the results? Add them to your code please so we see what you do. – blackapps Jun 07 '21 at 07:36
  • I have addedFile.exists() and File.canWrite() but the result is the same like java.io.FileNotFoundException: /cache/xyz/test.txt: open failed: EACCES (Permission denied) – GNK Jun 07 '21 at 07:40
  • Please put all those checks in your post. And tell the results. Write clear code please. Start with that directory. Only when you are done with the directory define something for your file. Step by step. You do all in one code line. So nobody knows what happens. – blackapps Jun 07 '21 at 07:45
  • I have modified the code, please check – GNK Jun 07 '21 at 07:59
  • Sorry you implemented that completely wrong. And you did not start with a destination directory but bumped in the target file directly. And did not check return value of canWrite() and not implemented consequenses for possible return values. – blackapps Jun 07 '21 at 10:23

1 Answers1

0

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

Son Tran
  • 11
  • 1
  • 1
    I have tried the above permissions also but not helped. I am developing an android system application. – GNK Jun 07 '21 at 07:37
  • That is for external storage. And /cache/xyz is no external storage. It does not even exist. But OP did not add code to show that. – blackapps Jun 07 '21 at 07:39
  • I am able to copy /cache/ , but not in /cache/xyz/ . – GNK Jun 07 '21 at 07:45