0

I want my Android application to create a public folder in the internal storage of the phone. I want to save some text files there that I want to be available even after the application uninstalls.

The goal is to create some txt that I want to transfer to my PC for further analysis. I wanted to try the Environment.DIRECTORY_DOCUMENTS, but the targeted phone is old and it doesn’t have the folder.

I have read the documentation but I can’t find any solution. I also tried to hard code the folder, but still it wont be created. I just want to have a folder like the picture camera folder.

I have tried this but it doesn't work.

Here is the code that I use:

File file = new File("/folder to create here", "name of file here" + File.pathSeparator + "txt");
if (!file.mkdirs())
    Log.e(getClass().toString(), "Save Data -> Directoy not created"); 
    
try {
    fos = new FileOutputStream(file);
    fos.write(saveString.toString().getBytes());
    fos.flush();
    fos.close();
}
catch (Exception e){
    if (e instanceof FileNotFoundException) {
        Toast.makeText(getApplicationContext(), "Could not create file, please try again", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    if (e instanceof IOException) {
        Toast.makeText(getApplicationContext(), "Could not write in file, please try again", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

Is there any way to do it or not. There are many application like viber that create folders in the root.

Thanks in advance for your help.

Community
  • 1
  • 1
Celiel
  • 1
  • 1

1 Answers1

0

As far as I know, there is no such mechanism to create a persist world writable directory. App's data directory in internal storage will be deleted after uninstall.

Since your device is connected with PC (Am I right?), here is my suggestion.

When your device is connected to your PC, in your PC, use adb tool to create a public writable directory under /data/local/tmp

adb shell mkdir /data/local/tmp/public_write
adb shell chmod 777 /data/local/tmp/public_write

And in your app, write the data you want to save to /data/local/tmp/public_wirte/data.txt

In you PC, you can use adb pull to access the file as long as your device is connected with your PC and the USB debug of your device is enable.

adb pull /data/local/tmp/public_write/data.txt
alijandro
  • 11,627
  • 2
  • 58
  • 74