新建的项目,一定要记得添加读写文件的权限啊,不然没有效果的,你不加权限,没有效果请不要找我
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
(1)我们首先需要创建assets文件夹,如图所示,在project状态下,鼠标选中main文件夹,右键单击,然后依次选择我们的内容,最后点击确定,assets就创建好了
然后我们在assets文件夹下面新建一个文件夹叫folder,把我们需要复制的文件放到此文件夹下面,我们放了三个不同的文件,如图所示:
(2)复制assets文件到内部存储中
package com.android.example.assets;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Utils{
public static void doCopy(Context context, String assetsPath, String desPath) throws IOException {
String[] srcFiles = context.getAssets().list(assetsPath);//for directory
for (String srcFileName : srcFiles) {
String outFileName = desPath + File.separator + srcFileName;
String inFileName = assetsPath + File.separator + srcFileName;
if (assetsPath.equals("")) {// for first time
inFileName = srcFileName;
}
Log.e("tag","========= assets: "+ assetsPath+" filename: "+srcFileName +" infile: "+inFileName+" outFile: "+outFileName);
try {
InputStream inputStream = context.getAssets().open(inFileName);
copyAndClose(inputStream, new FileOutputStream(outFileName));
} catch (IOException e) {//if directory fails exception
e.printStackTrace();