Android:下载并分享html文件问题

时间:2013-07-11 07:47:37

标签: android android-intent

在我的应用程序中,我想下载并共享一个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应用程序。

而且进度条也无效......

请告诉我分享下载文件的最佳方式..

1 个答案:

答案 0 :(得分: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"));