0

在我的应用程序中,我想下载并共享一个 html 文件。所以我尝试了以下代码:

Button download = (Button) findViewById(R.id.downloadbtn);
        download.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String file_url= "/* http url*/";
                 new DownloadFileFromURL().execute(file_url);

            }
        });

}


class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                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);
                String filePath = Environment.getExternalStorageDirectory().toString() + "/sridhar.html";
                // Output stream to write file
                OutputStream output = new FileOutputStream(filePath);

                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;
        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
       }

        /**
         * After completing background task
         * Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);

            goShare();
        }

    }
    public void goShare(){
         String filePath = Environment.getExternalStorageDirectory().toString() + "/sridhar.html";
            // setting downloaded into image view
         Log.e("Downloaded path","Path is "+filePath);
         Uri fileuri = Uri.fromFile(getFileStreamPath(filePath));


          //  MimeTypeMap myMime = MimeTypeMap.getSingleton();
            //String mimeType = myMime.getMimeTypeFromExtension("html");
            //File file = new File(imagePath);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
           // shareIntent.setDataAndType(Uri.fromFile(file),mimeType);
           // shareIntent.setFlags(shareIntent.FLAG_ACTIVITY_NEW_TASK);
            shareIntent.putExtra(Intent.EXTRA_STREAM, fileuri);
            shareIntent.setType("text/html");
            startActivity(Intent.createChooser(shareIntent,"Share"));
    }

问题是,我可以下载我的 html 文件,但我不能分享...即我可以带来共享上下文菜单并选择 gmail,该文件不被 gmail 应用程序接受,它会强制 gmail 应用程序关闭。

而且进度条也不起作用...

请为我提供分享下载文件的最佳方式..

4

1 回答 1

0

我找到了解决方案:

Intent shareIntent = new Intent();
 shareIntent.setAction(Intent.ACTION_SEND);
 shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
 shareIntent.setType("text/html");
 startActivity(Intent.createChooser(shareIntent,"Share"));
于 2013-07-11T13:19:23.017 回答