1
URLからファイルをダウンロードしています。ダウンロードから通知が完了したらファイルマネージャを開きます。そのユーザーは、通知をクリックしてファイルマネージャーからファイルを開くことができます。通知をクリックすると、何もしません。私はアンドロイドの初心者ですので、それほど多くの知識を持っていない誰も私はそれを行う方法を助けることができます。通知からファイルマネージャを開く
ここに私のコードです。
public class DownloadFileFromURL extends AsyncTask<String, String, String> {
private static final String LOG_TAG = "Directory";
Context context;
ProgressDialog pDialog;
Activity activity;
NotificationManager mNotifyManager;
Builder mBuilder;
int id=1;
String fileName;
public DownloadFileFromURL(Context context){
this.context = context;
}
/**
* Before starting background thread
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
pDialog = new ProgressDialog(context);
pDialog.setMessage("Downloading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new android.support.v7.app.NotificationCompat.Builder(context);
mBuilder.setContentTitle("Assignment")
.setContentText("Download in progress").setSmallIcon(R.drawable.download);
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
});
}
@Override
protected void onProgressUpdate(String... values) {
mBuilder.setProgress(100, Integer.parseInt(values[0]), false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File myDir = new File(root + "/saved_assignments");
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
String raw = conection.getHeaderField("Content-Disposition");
fileName = raw.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
if(fileName.contains("=")){
int counter=0;String str="";
for(int i=0; i<fileName.length();i++){
if(fileName.charAt(i)=='='){
counter = 1;
}else if(counter==1){
str =str + fileName.charAt(i);
}
}
fileName = str ;
}
String PATH = Environment.getExternalStorageDirectory().toString()+ "/saved_assignments/";
File folder = new File(PATH);
if(!folder.exists()){
folder.mkdir();//If there is no folder it will be created.
}
String file = PATH+fileName;
File fileNew = new File(file);
try {
fileNew.createNewFile();
} catch (IOException e) {
Log.e(LOG_TAG, "Directory not created");
}
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream());
// Output stream to write file
OutputStream output = new FileOutputStream(fileNew);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// 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;
}
/**
* After completing background task
* **/
@Override
protected void onPostExecute(String file_url) {
Toast.makeText(context,"Assignment saved in downloads",Toast.LENGTH_SHORT).show();
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
pDialog.hide();
}
}
はhttps://stackoverflow.com/questions/6781306/notification-bar-when-clicked-how-to-open-file-explorer-with-specifiedを参照してください。 -locati – sasikumar
私はこの解決策を試した –