Phonegap - How to save .txt file in root directory of android mobile

我怕爱的太早我们不能终老 提交于 2020-01-02 05:14:09

问题


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.


回答1:


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.




回答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




回答3:


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();



回答4:


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();
}


来源:https://stackoverflow.com/questions/41479513/phonegap-how-to-save-txt-file-in-root-directory-of-android-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!