我正在使用下面的代码将文件从资产复制到 SD 卡。
代码:
File file3 = new File("/sdcard/Alone.mp4");
if(!(file3.exists())) {
ArrayList<String> files = new ArrayList<String>();
files.add("Alone.mp4");
new myAsyncTask().execute(files);
}
// AsyncTass for the Progress Dialog and to do Background Process
private class myAsyncTask extends AsyncTask<ArrayList<String>, Void, Void> {
ArrayList<String> files;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(MainScreenActivity.this, "Speech Tutor", "Loading...");
}
@Override
protected Void doInBackground(ArrayList<String>... params) {
files = params[0];
for (int i = 0; i < files.size(); i++) {
copyFileFromAssetsToSDCard(files.get(i));
} return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}
}
// Function to copy file from Assets to the SDCard
public void copyFileFromAssetsToSDCard(String fileFromAssets){
AssetManager is = this.getAssets();
InputStream fis;
try {
fis = is.open(fileFromAssets);
FileOutputStream fos;
// if (!APP_FILE_PATH.exists()) {
// APP_FILE_PATH.mkdirs();
// }
fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(),fileFromAssets));
byte[] b = new byte[8];
int i;
while ((i = fis.read(b)) != -1) {
fos.write(b, 0, i);
}
fos.flush();
fos.close();
fis.close();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
现在,使用上面的代码,我可以将文件从资产复制到 sdcard。但我想要的不是将复制文件存储到 sdcard 中可用的另一个目录中。
那怎么办??
谢谢。