2013-07-11 13 views
0

내 응용 프로그램에서 하나의 html 파일을 다운로드하고 공유하고 싶습니다. 내가 그 ... 즉, 내가 할 수있는 공유 컨텍스트 메뉴를 선택 Gmail을 가져올 수는 공유하지 못할Android : 다운로드 및 공유 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 응용 프로그램을 강제 종료합니다.

또한 진행 표시 줄이 작동하지 않습니다

...

답변

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"));