【发布时间】:2017-08-28 20:36:14
【问题描述】:
在下面的代码中,当下载完成并且用户点击通知然后它直接进入文件管理器并打开“我的应用程序”文件夹
在文件夹图像文件中,有 PDF 以及 excel、word 或 zip 文件。
文件夹路径是:/storage/emulated/0/My Application
我的代码是:
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"));
}
}
【问题讨论】:
-
请具体说明您想要什么?
-
我需要这个文件夹中所有可用的文件
-
Stack Overflow 用于编程问题。你的问题是什么?如果您的问题是“我该怎么做?”,请说明您尝试了什么以及遇到了哪些具体问题。您可以通过
File类使用Java 文件I/O 来列出给定目录中的文件。 -
我用代码更新了我的问题,你能告诉我它来解决我的问题吗?