OOPDeveloper89
OOPDeveloper89

Reputation: 227

How to move file to download folder?

In my app I get base64 data of a file (doc, pdf, jpeg,...) and write it with FileWriter. The file is now in my app-directory. But I need to move it to the Download directory of Android device. Is it not possible to get access with this code:

 window.resolveLocalFileSystemURL('content:///storage/emulated/0/Download', function(a) {},fail}; 

I always get error code 1.

In my android manifest.xml I currently just have WRITE_EXTERNAL_STORAGE permission. Is this enough?

Thanks. By the way I use cordova3.4 and android 4.3.

Upvotes: 1

Views: 2850

Answers (4)

Jijo John
Jijo John

Reputation: 1

var destinationPath = cordova.file.externalRootDirectory + "Download/"; // Destination is Download folder window.resolveLocalFileSystemURL(storageLocation, function (fileEntry) {

    // Get the destination directory

    window.resolveLocalFileSystemURL(destinationPath, function (dirEntry) {

        // Move the file

        fileEntry.moveTo(dirEntry, fileName, function (newFileEntry) {

            console.log("File moved successfully to: " + newFileEntry.nativeURL);

        }, function (error) {

            console.error("Error moving file: " + error.code);

        });

    }, function (error) {

        console.error("Error resolving destination directory: " + error.code);

    });

}, function (error) {

    console.error("Error resolving source file: " + error.code);

});

Upvotes: 0

MikeB
MikeB

Reputation: 788

Why not just use the Cordova plugin: cordova-plugin-file. The specific file location is: cordova.file.externalRootDirectory + "Download/". So apply window.resolveLocalFileSystemURL() to that.

Upvotes: 0

QuickFix
QuickFix

Reputation: 11721

The sdcard path changes from device to device.

I suggest you read the File System plugin doc for complete information.

If you haven't modified config.xml to add <preference name="AndroidPersistentFileLocation" value="Internal" />, I'd say that this should do the job :

window.resolveLocalFileSystemURI('/Download', function(dirEntry) {},fail};

Or an other way :

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
    function (fs) {
        fs.root.getDirectory("/Download", {
            create : false,
            exclusive : false
        }, function (dirEntry) { //success
            //do here what you need to do with Download folder
        }, function () {
            //error getDirectory
        });
    },
    function () {
    //error requestFileSystem
    }
);

Upvotes: 1

user3514786
user3514786

Reputation: 11

this ll help u in Cordova to store file to ur requrired destination

public class FileDownload extends CordovaPlugin {

int serverResponseCode = 0;

ProgressDialog dialog = null;

String upLoadServerUri = null;
final String DownloadFilePath = null;
final String DownloadFileNameOne = null;
private final String PATH = "/storage/sdcard0/DestinationFoloder/";
public static final String ACTION_FILE_UPLOAD = "fileUploadAction";



@Override
public boolean execute(String action, JSONArray args,CallbackContext callbackContext) throws JSONException {
   try {
        if (ACTION_FILE_UPLOAD.equals(action)) {
            File dir = new File("/storage/sdcard0/Destination");
            if (dir.mkdir()) {

                Log.e("Directory created","DC");

            } else {
                Log.e("Directory Not created","DNC");

            }
            JSONObject arg_object = args.getJSONObject(0);
            String DownloadFilePath = arg_object.getString("path");
            String DownloadFileNameOne = arg_object.getString("NameDwn");
            fdownload(DownloadFilePath,DownloadFileNameOne);

            callbackContext.success("Attachment Download Sucessfully!");

        }
        return false;
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
        callbackContext.error(e.getMessage());
        return false;

    }

}



private boolean fdownload(String downloadFilePath2, String downloadFileNameOne2) {
    File FileCheck = new File(PATH+"/"+downloadFileNameOne2);

    if(FileCheck.exists()){

        return false;


    }else{

        try {
            URL url = new URL(downloadFilePath2); // you can write here
            // any
            // link
            Log.e("FilePath", "" + PATH + "" + downloadFileNameOne2);
            File file = new File("" + PATH + "" + downloadFileNameOne2);

            long startTime = System.currentTimeMillis();
            URLConnection ucon;
            ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            /*
             * Read bytes to the Buffer until there is nothing more to
             * read(-1).
             */
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }




    }

}

}

Upvotes: 0

Related Questions