Android copy assets目录下文件到手机SDCard上

如何在一个新建的工程里面添加assets目录:

Android copy assets目录下文件到手机SDCard上_第1张图片

拷贝assets下文件到sd卡主要方法:

/**

* 拷贝assets中的文件到SDCard中

*/

public boolean saveAssetsToSDCard(String assFileName, String localFileName) {

    boolean isSave;

    String fileDir = Environment.getExternalStorageDirectory() +"/test/";

    File fileD =new File(fileDir);

    if (!fileD.exists() || !fileD.isDirectory()) {

if (!fileD.mkdirs()) {

// 文件夹创建失败

            Log.i("copy file:", "saveAssetToSDCard: file make dir file");

        }

}

String filePath = fileDir + pathName;

    if (TextUtils.isEmpty(filePath)) {

return false;

    }

File file =new File(filePath);

    //如果文件存在,则不拷贝

    //if (file.exists()) {

//    return true;

//}

    InputStream is =null;

    FileOutputStream fos =null;

    try {

is = getApplicationContext().getAssets().open(assName);

        int size = is.available();

        byte[] buffer =new byte[size];

        is.read(buffer);

        fos =new FileOutputStream(file);

        fos.write(buffer);

        isSave =true;

        Log.i("copy file:", "saveAssetToSDCard: finish");

    }catch (Exception e) {

isSave =false;

        e.printStackTrace();

    }finally {

if (is !=null) {

try {

is.close();

            }catch (Exception e) {

e.printStackTrace();

            }

}

if (fos !=null) {

try {

fos.close();

            }catch (Exception e) {

e.printStackTrace();

            }

}

}

return isSave;

}

你可能感兴趣的:(Android copy assets目录下文件到手机SDCard上)