【问题标题】:Click On Notification Download Completion It Opens downloaded all files of particular folder单击通知下载完成它打开下载的特定文件夹的所有文件
【发布时间】: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 来列出给定目录中的文件。
  • 我用代码更新了我的问题,你能告诉我它来解决我的问题吗?

标签: android android-asynctask


【解决方案1】:

首先,您需要设置一个将在点击notification 时打开的活动,然后您需要一个ListView 以在您的ListView 中显示所需的文件。

以下代码可能会对您有所帮助。它可以帮助您查看列表视图中的文件和

public void onCreate(Bundle savedInstanceState) 
 {
super.onCreate(savedInstanceState);

myList = new ArrayList<String>();   

String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/external_sd" ) ;       
File list[] = file.listFiles();

for( int i=0; i< list.length; i++)
{
    myList.add( list[i].getName() );
}

setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myList ));

}

根据您要执行的操作实现 onClick 方法。

希望对你有帮助。

【讨论】:

  • 我用我已经完成的代码更新了我的问题,你能根据我的代码帮助我吗?
  • 基本上你不想打开下载管理器。您想通过文件列表显示您自己的活动吗?我说的对吗?
  • 是的,我不想在 listview 中看到文件,我只想在下载后去存储我的文件的那个地方
猜你喜欢
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多