的所有文件,在下面的代碼,當下載完成,並於通知用戶點擊即可直接進入文件管理器,打開「我的應用程序」文件夾點擊通知下載完成它打開下載特定文件夾
在文件夾中的圖像文件,PDF以及excel,word或zip文件在那裏。
文件夾路徑是:/存儲/模擬/ 0 /我的應用
我的代碼是:
ProgressDialog pDialog; public static final int progress_bar_type = 0;所有的
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
build.setProgress(100, 0, false);
mNotifyManager.notify(ID, build.build());
pDialog = new ProgressDialog(activity);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
String NoticeUrl = "http://" + DOMAIN +"//NoticeBoard/";
System.out.println("============ Notice : =============" + NoticeUrl);
Log.d("file name exec", f_url[0] + "");
URL url = new URL(NoticeUrl
+ Uri.encode(f_url[0].trim()));
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),
8192);
direct = new File(Environment.getExternalStorageDirectory()+"/My Application");
if(!direct.exists()) {
if(direct.mkdir()); //directory is created;
}
// Output stream to write file
OutputStream output = new FileOutputStream(
direct + "/" + f_url[0].trim());
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();
// Displaying downloaded image into image view
// Reading image path from sdcard
FilePath = direct + "/" + f_url[0].trim();
// setting downloaded into image view
//my_image.setImageDrawable(Drawable.createFromPath(imagePath));
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
int i;
for (i = 0; i <= 100; i += 5) {
// Sets the progress indicator completion percentage
publishProgress(String.valueOf(Math.min(i, 100)));
try {
// Sleep for 5 seconds
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
Log.d("Failure", "sleeping failure");
}
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
build.setProgress(100, Integer.parseInt(progress[0]), false);
mNotifyManager.notify(ID, build.build());
super.onProgressUpdate(progress);
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String f_url) {
// dismiss the dialog after the file was downloaded
pDialog.dismiss();
Toast.makeText(activity, "File Downloaded at : - " + FilePath , 4600).show();
System.out.println("==================" + FilePath);
Toast.makeText(activity, "Download Completed", 200).show();
// build.setSmallIcon(android.R.drawable.ic_dialog_alert);
/*File file = new File(FilePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "");
PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplication(), 0, intent, 0);
build.setContentIntent(pendingIntent);
build.setLargeIcon(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_launcher));*/
build.setContentText("Download complete");
build.setProgress(0, 0, false);
mNotifyManager.notify(ID, build.build());
//Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//Uri uri = Uri.parse(FilePath); // a directory
//intent.setDataAndType(uri, "*/*");
//activity.startActivity(Intent.createChooser(intent, "Open folder"));
}
}
請具體說明你想要什麼? – Sahil
我需要此文件夾中可用的所有文件的文件 –
Stack Overflow用於編程問題。你的問題是什麼?如果您的問題是「我該如何做到這一點?」,請解釋您嘗試過的以及您遇到的具體問題。您可以通過'File'類使用Java文件I/O來列出給定目錄中的文件。 – CommonsWare