2017-10-19 2 views
0

ダウンロードマネージャーがファイルをダウンロードする前に、ファイル名に "+"記号と "_"と "/"記号を "="で置き換える方法はありますか?onDownloadStartで文字列を変更するにはどうすればよいですか?

@Override 
     public void onDownloadStart(String url, String userAgent, 
            String contentDisposition, String mimeType, 
            long contentLength) { 
      DownloadManager.Request request = new DownloadManager.Request(
        Uri.parse(url)); 
      request.setMimeType(mimeType); 
      String cookies = CookieManager.getInstance().getCookie(url); 
      request.addRequestHeader("cookie", cookies); 
      request.addRequestHeader("User-Agent", userAgent); 
      request.setDescription("Downloading file..."); 
      request.setTitle(URLUtil.guessFileName(url, contentDisposition, 
        mimeType)); 
      request.allowScanningByMediaScanner(); 
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
      request.setDestinationInExternalPublicDir(
        Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
          url, contentDisposition, mimeType)); 
      DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      dm.enqueue(request); 
+0

同じ質問を2回尋ねることは、あなたに別の答えを与えるつもりはありません。私はすでにあなたの質問に答えました。私はそれをテストし、それは働いた。あなたは質問を削除しました.... –

答えて

2

あなたの文字列/ファイル名があることを前提としています:

String myFileName = "basal_metabolic_rate+example.pdf"; 

あなたは次のようにして_+を置き換えることができます。

String result = myFileName.replaceAll("[+]", "_"); 

文字列result今すぐ返します。

basal_metabolic_rate_example.pdf


同じあなたが行うことができ、 "=" と "/" 置き換えたいときのために行く:

String myFileName = "basal/metabolic/rate/example.pdf"; 
String result = myFileName.replaceAll("[/]", "="); 

// Your file will now be basal=metabolic=rate=example.pdf 

EDIT:どのようにここで

ですあなたのコードに実装することができます:

@Override 
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) { 

     //You can first get the filename: 
     String filename = URLUtil.guessFileName(url, contentDisposition, mimeType); 
     //Then you can replace the "+" with "_" 
     String replacePlus = filename.replaceAll("[+]", "_"); 
     //Then you can replace the "/" with "=" 
     String replaceSlash = replacePlus.replaceAll("[/]", "="); 

     DownloadManager.Request request = new DownloadManager.Request(
     Uri.parse(url)); 
     request.setMimeType(mimeType); 
     String cookies = CookieManager.getInstance().getCookie(url); 
     request.addRequestHeader("cookie", cookies); 
     request.addRequestHeader("User-Agent", userAgent); 
     request.setDescription("Downloading file..."); 

     //Now here where you set the title you can set it to the last edited string, like this: 
     request.setTitle(replaceSlash); 

     request.allowScanningByMediaScanner(); 
     request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

     //And here where you set the destination directory 
     request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, replaceSlash); 

     DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
     dm.enqueue(request); 
+0

男私はどのように私のコードでこれを実装するのですか?それは私にエラーを与える。あなたは本当にそれを "テスト"しましたか? –

+0

はい私はそれをテストし、それは動作します。あなたはこれをどこで実装したのですか? –

+0

私の編集を参照してください、これは私がそれを実装する方法です。私もコメントを追加しました。 –

関連する問題