AccesDeniedException, in pure java on Android 8.1 with AIDE

2

I have a pure java program that encrypts files from a specific directory (in this case the sdCard) and I'm running it on Android Oreo 8.1 with the IDE for Android called AIDE (it's not an Android application, it's pure Java) , and there is a block of code that deletes the original file but I have an exception. What can I do?

CODE:

public static void deleteFiles( String file ) throws IOException
    {
    try
    {
        Files.deleteIfExists( Paths.get( file ) );
    }
    catch( NoSuchFileException e )
    {
        System.out.println("No such file exists");
    }
    }

This is the error that launches:

  

java.nio.file.AccessDeniedException:   /storage/4A80-16E6/qwerty/test.mp4 at   sun.nio.fs.UnixFileSystemProvider.implDelete (UnixFileSystemProvider.java:244)     at   sun.nio.fs.AbstractFileSystemProvider.deleteIfExists (AbstractFileSystemProvider.java:108)     at java.nio.file.Files.deleteIfExists (Files.java:1165) at   encryptiondecryptionfile.ExecCrypto.deleteFiles (ExecCrypto.java:226)     at encryptiondecryptionfile.ExecCrypto.encrypt (ExecCrypto.java:97)     at encryptiondecryptionfile.ExecCrypto.main (ExecCrypto.java:34) at   java.lang.reflect.Method.invoke (Native Method) at   com.aide.ui.build.java.RunJavaActivity $ 1.run (SourceFile: 108) at   java.lang.Thread.run (Thread.java:764)

THE PACK OF THE FOLDER IS: /storage/4A80-16E6/qwerty/test.mp4

    
asked by Ivan Slipkorn 07.05.2018 в 15:51
source

1 answer

0

As the error info is telling you, this is a exception of type AccessDeniedException .

To begin with I would try to get information about the exception : cause, etc ...

For this you can add the treatment for this type of exceptions:

catch( AccessDeniedException e1 )
    {
        System.err.println(e.getFile());
        System.err.println(e.getMessage());
        System.err.println(e.getReason());

    }

So your code would look like this:

public static void deleteFiles( String file ) throws IOException
{
try
{
    Files.deleteIfExists( Paths.get( file ) );
}
catch( NoSuchFileException e )
{
    System.out.println("No such file exists");
}
catch( AccessDeniedException e1 )
    {
        System.err.println(e.getFile());
        System.err.println(e.getMessage());
        System.err.println(e.getReason());

    }
}

I hope it's useful.

    
answered by 07.05.2018 в 16:42