2017-05-31 9 views
0

現在、特定のURLからデバイスの外部ストレージまたはmicrosdにファイルをダウンロードし、ダウンロードが完了した後にファイルを開こうとしています。ファイルを外部ストレージにダウンロードして開きます

// execute this when the downloader must be fired 
    final DownloadTask downloadTask = new DownloadTask(MainActivity.this); 
    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, permission_external_memory); 
     return; 
    } else { 
     downloadTask.execute("http://www.sample-videos.com/doc/Sample-doc-file-100kb.doc"); 
    } 

そして、ここでは私のダウンローダasynctaskコードです:私は私のOnCreate関数の内部にある。ここhere

からの回答を追っ

private class DownloadTask extends AsyncTask<String, Integer, String> { 

    private Context context; 
    private PowerManager.WakeLock mWakeLock; 

    public DownloadTask(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // take CPU lock to prevent CPU from going off if the user 
     // presses the power button during download 
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
       getClass().getName()); 
     mWakeLock.acquire(); 
     mProgressDialog.show(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     // if we get here, length is known, now set indeterminate to false 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgress(progress[0]); 
    } 

    @Override 
    protected String doInBackground(String... sUrl) { 
     InputStream input = null; 
     OutputStream output = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      // expect HTTP 200 OK, so we don't mistakenly save error report 
      // instead of the file 
      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 

      // this will be useful to display download percentage 
      // might be -1: server did not report the length 
      int fileLength = connection.getContentLength(); 

      // download the file 
      input = connection.getInputStream(); 
      File mypath = new File(Environment.getExternalStorageDirectory(), "myfiletest.doc"); 
      output = new FileOutputStream(mypath); 

      byte data[] = new byte[4096]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       // allow canceling with back button 
       if (isCancelled()) { 
        input.close(); 
        return null; 
       } 
       total += count; 
       // publishing the progress.... 
       if (fileLength > 0) // only if total length is known 
        publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
      } 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (output != null) 
        output.close(); 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     mWakeLock.release(); 
     mProgressDialog.dismiss(); 
     if (result != null) 
      Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show(); 
     else { 
      Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_VIEW); 
      intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory(), "myfiletest.doc"), "text/*"); 
      context.startActivity(intent); 
     } 
    } 

} 

しかし、ダウンロードが完了した後、私は開くことができません。ファイルと私はまた、ファイルマネージャでダウンロードしたファイルを見つけることができません。ダウンロードが完了してファイルを開こうとしたときに表示されるダイアログです: enter image description here

ここで何が間違っていましたか?

+0

に保存されましたか? – Kaushal28

+0

http://www.sample-videos.com/doc/Sample-doc-file-100kb.doc – user3576118

+0

書き込み権限がありますか? – Kaushal28

答えて

1

あなたのコードを試しました。それは完全代わりにこのラインの作業をされています

intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory(), "myfiletest.doc"), "text/*"); 

は、だから私はそれを変更:

intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/myfiletest.doc"), "text/*"); 

そしてまた、すべての必要な権限を含め、設定ANSから明示的にすべての権限を与えられました。ダウンロード後、ダウンロードしたファイルを開くアプリを使用するかどうか尋ねられます。適切なオプションを選択すると、ファイルが開きます。

したがって、明示的なアクセス許可が不足しています。乾杯!

EDIT: あなたが使用しているURLを提供することができますそれは/storage/emulated/0/

+0

申し訳ありませんが、私はそれをよく理解していません。はい、ダウンロード後、ダウンロードしたファイルを開くアプリケーションを使用するかどうかを尋ねます。私もその行を変更しようとします。しかし、ダウンロードしたファイルをデバイスのファイルマネージャから、またはUSBケーブルを使ってコンピュータから閲覧すると、ダウンロードしたファイルが見つからないようです。私も必要な許可を確認したと思います。 – user3576118

+0

私の場合、これらの変更の後でコードは完全に動作しています。編集を参照してください。 – Kaushal28

+0

まだファイルが見つからない場合は、その回答を削除します。 – Kaushal28

関連する問題