Phonegap - How to save .txt file to Android Android root directory

I am trying to save a .txt file to the root directory of an Android mobile phone using a phone splash screen. I installed these plugins "cordova-plugin-file" and "cordova-plugin-file-transfer". In the 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>

The code to handle the 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 a successful callback, it returns this file location.

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

But when I try to find this file in this place. He is not there. I want this file in my mobile root directory.

+4
source share
4 answers

.

cordova , . .

readme. , . SO Post .

+2

rooting root . , SD-. /sdcard/ .

, :

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

/data/data​​p >

+2

, .

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: , ,

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*/

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

FileWriter writer = new FileWriter(myFile);
writer.write("this is my first line in the file");
writer.close();
0
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();
}
-2

Source: https://habr.com/ru/post/1665833/


All Articles