4

I m trying to save a .txt file in root directory of android mobile using phonegap. I have installed these plugins 'cordova-plugin-file' and 'cordova-plugin-file-transfer'. In config.xml file i add this preference.

<preference name="AndroidPersistentFileLocation" value="Internal" />

Here is my code.

<button class="btn-lg btn-success" onclick="saveFile('hello_world.txt', 'This is Dummy Content')">Write File</button>

Code to handle on click event.

function saveFile(fileName, fileData) {
    // Get access to the file system
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
        // Create the file.
        fileSystem.root.getFile(fileName, {create: true, exclusive: false}, function (entry) {
            // After you save the file, you can access it with this URL
            var myFileUrl = entry.toURL()
            entry.createWriter(function (writer) {
                writer.onwriteend = function (evt) {
                    alert("Successfully saved file to " + myFileUrl)
                }
                // Write to the file
                writer.write(fileData)
            }, function (error) {
                alert("Error: Could not create file writer, " + error.code)
            })
        }, function (error) {
            alert("Error: Could not create file, " + error.code)
        })
    }, function (evt) {
        alert("Error: Could not access file system, " + evt.target.error.code)
    })
}

After success callback it return this file location.

file:///data/data/com.adobe.phonegap.app/files/files/hello_world.txt

But when i try to find this file in given location. Its not there. I want this file in my root directory of mobile.

Pravin Pawar
  • 2,559
  • 3
  • 34
  • 40
Owais Kiani
  • 355
  • 1
  • 6
  • 26

4 Answers4

2

Without rooting or using the root permission you cannot write to the root directory of the device. Maybe you want to write to the sd card. In that case write to /sdcard/ that should be possible when you setup the correct permissions.

If you want to access your local app data you could run this command here:

adb shell run-as com.adobe.phonegap.app ls -laR

However normally you cannot look into directories within /data/data

rekire
  • 47,260
  • 30
  • 167
  • 264
2

Check out this link that contains the working sample.

The link contains sample cordova app for file download, save and open. Supports folder deletion option as well.

Check out the readme section. Hope it helps. Also you can check out this SO Post for more info on this.

Community
  • 1
  • 1
Gandhi
  • 11,875
  • 4
  • 39
  • 63
0

I usually use 1 of the two options to save file, save it in home directory of internal storage or home directory of my app.

option1:

String directory = Environment.getExternalStorageDirectory().getAbsolutePath();
/* it will give you home directory of internal storage like this /storage/emulated/0*/ 


String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
/* it will give DCIM folder directory /storage/emulated/0/DCIM */

String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
/* it gives /storage/emulated/0/Pictures */

option2: or you can use directory of your application where mobile user can't see this files below is the code for this

ContextWrapper wrapper = new ContextWrapper(this);
String directory = wrapper.getDir("MyTempFolder", ContextWrapper.MODE_PRIVATE).toString();
/*directory will be of /data/data/com.your_domain_name.your_project_name/app_MyTempFolder*/

and to save file use below code

File myFile = new File(directory, "MyFile.txt");
if(!myFile.exists())
    myFile.createNewFile();

and if you want to write some data in that file

FileWriter writer = new FileWriter(myFile);
writer.write("this is my first line in the file");
writer.close();
Nadimuddin
  • 230
  • 3
  • 17
-2
String path = this.getApplicationContext().getFilesDir() + "/testDir/";
File file = new File(path);
file.mkdirs();
path += "testlab.txt";
OutputStream myOutput;
try {
myOutput = new BufferedOutputStream(new FileOutputStream(path,true));
write(myOutput, new String("TEST").getBytes());
myOutput.flush();
myOutput.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Amado
  • 371
  • 1
  • 16