如何创建目录并将文件从资产文件夹存储到其中?
我使用下面的代码将文件从资产复制到 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();
}
}
现在,通过上面的代码,我可以将文件从资产复制到 SD 卡。但我想要的不是将副本文件存储到 SD 卡中可用的另一个目录中。
那么该怎么办呢??
谢谢。
I am using below code to copy the file from assets to the Sdcard.
code:
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();
}
}
Now, with above code i am able to copy the file from assets to the sdcard. but instead of that i want is to store the copy file in to another directory available in sdcard.
So how to do it ??
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想将文件存储在SD卡的某个特定文件夹中,则以下是代码。
Environment.getExternalStorageDirectory() + "/folderName/"
如果您希望创建一个新文件夹,则以下是代码。
If you want to store the file in some specific folder of sdcard, then following is the code.
Environment.getExternalStorageDirectory() + "/folderName/"
And If you are looking forward to create a new folder, then following is the code.