我写了这个代码,用于在我的Android应用程序中下载一些文件,但现在下载的文件在根目录中没有任何特殊顺序,看起来非常糟糕!所以我想知道如何为我的应用程序的下载文件添加文件夹?
解释输出路径的代码行:
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + downloadedfileName);
这是我的代码:
private class DownloadFileFromURL extends AsyncTask<String, String, String> {
private String downloadedfileName;
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "بارگذاری...", Toast.LENGTH_SHORT).show();
pDialog = new ProgressDialog(DownloadActivity.this);
pDialog.setMessage("کتاب در حال بارگذاري، لطفا منتظر بمانيد...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
pDialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(DownloadActivity.this)
.setTitle("دانلود کتاب با موفقیت انجام شد")
.setMessage("از دانلود شما سپاسگذاریم.")
.setNeutralButton("خواهش میکنم", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(DownloadActivity.this, LibActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
DownloadActivity.this.startActivity(i);
DownloadActivity.this.finish();
}
});
builder.create().show();
}
public void setDownloadedFileName(String downloadedfileName){
this.downloadedfileName = downloadedfileName;
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... surl) {
int count;
try {
URL url = new URL(surl[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a typical 0-100%
// progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
//OutputStream output = new FileOutputStream(Environment
// .getExternalStorageDirectory().toString()
// + "/data/" + downloadedfileName);
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/ketabha/" + downloadedfileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
// publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
}
答案 0 :(得分:1)
只需将此更改为您喜欢的目录:
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/ANYFOLDER/ANOTHERFOLDER/" + downloadedfileName);
请记住,在第一次使用此代码创建该目录时:
File dir = new File(yourdirectory);
if(!dir.exists()){
dir.mkdirs();
}
如果您在AndroidManifest.xml
文件中添加了相关的清单,那么上面的代码就可以了:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
有关更多信息,请参阅以下链接:
How to create directory automatically on SD card
Create folder in Android
can't create a folder in external storage on android device