Tristán Belmont
Tristán Belmont

Reputation: 11

Files just creating once on Android 11

i have an app that take some pictures and print some logs with the logback-android library, this pictures and logs was saved in a folder in the root directory storage/emulated/0/my-directory/ But we are updating to Android 11 and the new storage policy dont let me save files in that route, so my pictures and logs are now stored in documents in this way:

val directory = File(getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "pictures")
if(directory.exists()) directory.deleteRecursively()                                                
fileName = this.getFileName(c);
File file = new File(directory, fileName);
OutputStream output = new FileOutputStream(file);
output.write(data);
output.flush();
LOGGER.debug("File {} written", fileName);
output.close();

And the logback configuration looks like this:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
        
    <file>/storage/emulated/0/documents/logs_proyect/log_proyect.txt</file>
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{40} - %msg%n</pattern>
        </encoder>
</appender>

The problem is that the files get saved correctly just once, if i delete the log file it stops writting logs forever, and even if i dont delete the file sometimes tops writting in it, and the same pass with the pictures, if i delete files it stop saving them. I cant use the all files acces permission for the google policy, my manifest have this permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.MANAGE_INTERNAL_STORAGE"/>

Thanks in advance for any help

Upvotes: 1

Views: 333

Answers (2)

porwalankit
porwalankit

Reputation: 481

Starting Android 11 we have to use Scoped Storage or MediaStore APIs Below is the link which explains how to use MediaStore Apis

https://gist.github.com/fiftyonemoon/433b563f652039e32c07d1d629f913fb

Upvotes: 1

akram abdullah
akram abdullah

Reputation: 164

Use MANAGE_EXTERNAL_STORAGE permission

Upvotes: 0

Related Questions