简体   繁体   English

将文件复制到外部sdcard android

[英]Copy file to external sdcard android

I cannot copy a file to the external sdcard. 我无法将文件复制到外部sdcard。 Does not show up any error, in-fact shows success, but the file is not on the sd card. 不显示任何错误,实际上显示成功,但文件不在SD卡上。 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. 因此最后它调用move_success。

NOTE: IT WORKS IF I CHANGE PATH FROM 'file:///mnt/extsd' to the internal sdcard path 'file:///mnt/sdcard' 注意:如果我将路径从'file:/// mnt / extsd'更改为内部sdcard路径'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: 您应该构建路径,而不是将其定义为默认路径,因为并非在所有设备中sdcard都被称为“ sdcard”,例如,我有一个chinnese平板电脑,并且它具有2个sdcard插槽,因此我必须使用路径“ sdcard2 “ 你能行的:

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: 或者,可以使用文件的属性namedTo()作为替代,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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