2016-06-02 6 views
0

オンラインソースからファイルをダウンロードしようとしています。私が抱えている問題は、ブラウザのウィンドウがダウンロードサーバーにロードされたままになることです。私はこれを隠すことができるかもしれない何らかの方法がありますか?私はすでにAsyncTaskdoInBackground部分にこのコードを持っていますが、ブラウザバーを隠すことはできません。ここに私のコードはあります:オンラインからファイルをダウンロードするときにブラウザを非表示にする方法

ありがとうございました!ここ

答えて

0

は私のコードのサンプル、ブラウザなしでダウンロードできます:

private TextView mFileDownloadProgressBarPercent; 
private ProgressBar mFileDownloadProgressBar; 
private Runnable mFileExecutionTaskAfterDownload;  
public String fileDownloadedResultPath; 

とasynctask:

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


     // Before starting background thread 
     // Show Progress Bar Dialog 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      if(mFileDownloadProgressBar != null) 
       mFileDownloadProgressBar.setVisibility(View.VISIBLE); 
      if(mFileDownloadProgressBarPercent != null) 
       mFileDownloadProgressBarPercent.setVisibility(View.VISIBLE); 
     } 


     // 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 extStorageDirectory = Environment.getExternalStorageDirectory() 
         .toString(); 
       File folder = new File(extStorageDirectory, "pdf"); // for example we are downloading pdf's so store in pdf dir. 
       folder.mkdir(); 
       File subFolder = new File(extStorageDirectory+"/pdf", "fileId"); // here you can place files by id of category etc.. 
       subFolder.mkdir(); 


       String fileName = url.toString().substring(url.toString().lastIndexOf("/")+1); 
       fileDownloadedResultPath = subFolder + "/" + fileName; 
       // Output stream to write file 
       OutputStream output = new FileOutputStream(subFolder + "/" + fileName); 

       byte data[] = new byte[1024]; 

       long total = 0; 

       while ((count = input.read(data)) != -1) { 
        total += count; 
        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; 
     } 

     protected void onProgressUpdate(String... progress) { 
      if(mFileDownloadProgressBar != null) 
       mFileDownloadProgressBar.setProgress(Integer.parseInt(progress[0])); 
      if(mFileDownloadProgressBarPercent != null) 
       mFileDownloadProgressBarPercent.setText(mContext.getString(R.string.downloading_file) + " " + String.format("%s%%",Integer.parseInt(progress[0])+"")); 
     } 

     @Override 
     protected void onPostExecute(String file_url) { 
      if(mFileDownloadProgressBar != null) 
       mFileDownloadProgressBar.setVisibility(View.GONE); 
      if(mFileDownloadProgressBarPercent != null) 
       mFileDownloadProgressBarPercent.setVisibility(View.GONE); 
      if(mFileExecutionTaskAfterDownload != null) 
       mFileExecutionTaskAfterDownload.run(); 
     } 

    } 
+0

私は 'DIRECTORY.Downloads'が、何かに書き込み位置を変え除いて、私は、あなたのコードを試してみましたまだ間違っています。ファイルをダウンロードしようとすると、このエラー「/Download/app/data/app-release-0.apk:open failed:ENOENT(そのようなファイルやディレクトリはありません)」が表示されます。これは、私はそれを修正する方法だけではわかりません。私はいくつかのGoogle検索を実行しますが、それを修正する方法を知っていれば私に知らせてください – Ethan

+0

外部ストレージを書き込む権利が必要です –

関連する問題