简体   繁体   English

将目录,子目录和文件复制到SD卡-Android

[英]Copy directory ,sub directories and files to sd card - android

I use the following code to copy a particular directory and its contents to the sd card. 我使用以下代码将特定目录及其内容复制到sd卡中。 The directory is placed inside res/raw folder. 该目录位于res/raw文件夹中。

Following is the code I use: 以下是我使用的代码:

public class CopyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    copyFilesToSdCard();
}

static String extStorageDirectory =  Environment.getExternalStorageDirectory().toString();
final static String TARGET_BASE_PATH = extStorageDirectory+"/Android/data/";

private void copyFilesToSdCard() {
    copyFileOrDir("");
}

private void copyFileOrDir(String path) {
    AssetManager assetManager = this.getAssets();
    String assets[] = null;
    try {
        Log.i("tag", "copyFileOrDir() "+path);
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            String fullPath =  TARGET_BASE_PATH + path;
            Log.i("tag", "path="+fullPath);
            File dir = new File(fullPath);
            if (!dir.exists())
                if (!dir.mkdirs());
                    Log.i("tag", "could not create dir "+fullPath);
            for (int i = 0; i < assets.length; ++i) {
                String p;
                if (path.equals(""))
                    p = "";
                else 
                    p = path + "/";

                    copyFileOrDir( p + assets[i]);
            }
        }
    } catch (IOException ex) {
        Log.e("tag", "I/O Exception", ex);
    }
}

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    String newFileName = null;
    try {
        Log.i("tag", "copyFile() "+filename);
        in = assetManager.open(filename);
        if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
            newFileName = TARGET_BASE_PATH + filename.substring(0, filename.length()-4);
        else
            newFileName = TARGET_BASE_PATH + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", "Exception in copyFile() of "+newFileName);
        Log.e("tag", "Exception in copyFile() "+e.toString());
    }

}
}

exception: 例外:

 Exception in copyFile() of /mnt/sdcard/Android/data/raw/edu/anees.txt
 Exception in copyFile() java.io.FileNotFoundException:
 /mnt/sdcard/Android/data/raw/edu/anees.txt: open failed: ENOENT (No such file or directory)

Can someone please let me know what causes this issue and solution for the same. 有人可以让我知道导致此问题的原因和解决方案。

ref: How to copy files from 'assets' folder to sdcard? 参考: 如何将文件从“资产”文件夹复制到sdcard?

EDIT 编辑

I could resolve the exception with one of the tips regarding permission which was posted as one answer here. 我可以使用有关许可的技巧之一来解决该异常,该技巧已在此处发布为一个答案。

Now I encounter another issue which is as follows: 现在我遇到另一个问题,如下所示:

The following log says I cannot create a folder in the following path: 以下日志显示我无法在以下路径中创建文件夹:

Log.i("tag", "could not create dir "+fullPath);// fullPath = /mnt/sdcard/Android/data/raw

I do not want to have the data stored inside /mnt/sdcard/Android/data/raw, but instead, I want to have the contents of the raw folder inside assets to be copied to the path /mnt/sdcard/Android/data which is not happening with the piece of code I use from the reference link I gave. 我不想将数据存储在/ mnt / sdcard / Android / data / raw内,但是,我希望将资产内部的原始文件夹的内容复制到路径/ mnt / sdcard / Android / data我从给定的参考链接中使用的那段代码没有发生这种情况。 Any obvious reasons that might cause this with the code I gave? 任何明显的原因可能会导致我给出的代码出现此问题?

您可能忘记了在清单中设置权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

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