简体   繁体   中英

Copy file to external sdcard android

I cannot copy a file to the external sdcard. Does not show up any error, in-fact shows success, but the file is not on the sd card. Code is as follows:

window.resolveLocalFileSystemURI(fileURI, step1,fail);

function step1(tmp_file)
{
       file = tmp_file;
       window.resolveLocalFileSystemURI("file:///mnt/extsd", step2,fail); //resolve destinaion
}

function step2(destination)
{
        file.moveTo(destination,"example.jpg",move_success, move_fail);
}

So on the end it calls move_success.

NOTE: IT WORKS IF I CHANGE PATH FROM 'file:///mnt/extsd' to the internal sdcard path 'file:///mnt/sdcard'

Permissions in manifest

<uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.WRITE_CONTACTS" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

You should build your path, not define it as default, cause not in all devices the sdcard is called "sdcard", for example, i have a chinnese tablet, and this has 2 sdcard slots, so i have to use the path "sdcard2" you can do it:

File sdcard = Environment.getExternalStorageDirectory();
String path = "file://"+sdcard.getAbsolutePath();

Then you can use the variable

window.resolveLocalFileSystemURI(path, step2,fail); //resolve destinaio

Or, you can use this method to copy a file:

public void copy(File src, File dst) throws IOException {
    try {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (IOException io) {
        Toast.makeText(this, "Error: " + io, Toast.LENGTH_LONG).show();
    }
}

You can copy the file, next delete it, as a fast move.

Or as other alternative, you can use the property renameTo() of the file, For example:

File sdcard = Environment.getExternalStorageDirectory();
File example= new File(sdcard.getAbsolutePath()+"/example.txt");
File newpath= new File(sdcard.getAbsolutePath()+"/examplefolder/example.txt");
example.renameTo(newpath);//this will move the file to the new path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM