2017-08-07 17 views
0

現在、私はWebViewでpdfを表示し、ダウンロード機能を実装しようとしています。私はそれを行うためにダウンロードマネージャを使用しています。 pdfファイルをダウンロードしたエミュレータで動作します。しかし、スマートフォンでは、「ダウンロード1タスク」が表示されました。数秒後、通知は何もダウンロードせずに消えた。私はApi 19以降で実装しています。私のスマートフォンはアンドロイド5.1です。この問題の解決策はありますか?スマートフォンでAndroid DownloadManagerが動作しない

PdfFullscreenActivity.java

public class PdfFullscreenActivity extends AppCompatActivity { 

public WebView webview; 
public String str; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pdf_fullscreen); 

    Intent intent = getIntent(); 
    str = intent.getExtras().getString("PDF_TAG"); 

    String url = "https://docs.google.com/viewer?embedded=true&url=https://spmsejarahscore.000webhostapp.com/web/media/pdf/" + str; 

    webview = (WebView)findViewById(webView); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.getSettings().setPluginState(WebSettings.PluginState.ON); 
    webview.getSettings().setBuiltInZoomControls(false); 
    webview.getSettings().setAllowFileAccess(true); 
    webview.getSettings().setAppCacheMaxSize(1024 * 1024); 
    webview.getSettings().setAppCacheEnabled(true); 
    webview.loadUrl(url); 

    webview.setWebViewClient(new WebViewClient() { 
     public void onPageFinished(WebView view, String url) { 
     } 

     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      return true; 
     } 
    }); 
} 

public void onBackPressed(){ 
    super.onBackPressed(); 
    finish(); 
} 

public void download(View v){ 
    // https://spmsejarahscore.000webhostapp.com/web/media/pdf/SijilPelajaranMalaysia(SPM)_2011_P1,2.pdf 

    String downloadUrl = "https://spmsejarahscore.000webhostapp.com/web/media/pdf/" + str; 

    Uri urifile = Uri.parse(downloadUrl); 
    downloadfile(urifile, v); 
} 

public long downloadfile(Uri uri, View view){ 
    long downloadReference; 
    DownloadManager downloadManager; 

    // Create request for android download manager 
    downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); 
    DownloadManager.Request request = new DownloadManager.Request(uri); 

    //Setting title of request 
    request.setTitle("Downloading"); 

    //Setting description of request 
    request.setDescription(str); 

    //Set the local destination for the downloaded file to a path within the application's external files directory 
    request.setDestinationInExternalFilesDir(PdfFullscreenActivity.this, Environment.DIRECTORY_DOWNLOADS, str); 

    //Enqueue download and save into referenceId 
    downloadReference = downloadManager.enqueue(request); 

    return downloadReference; 
} 
} 

activity_pdf_fullscreen.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.foong.spmsejerahscore.PdfFullscreenActivity"> 

<WebView 
    android:id="@+id/webView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ImageButton 
    android:id="@+id/downloadbtn" 
    android:layout_width="45dp" 
    android:layout_height="45dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginTop="10dp" 
    android:layout_marginRight="10dp" 
    android:onClick="download" 
    android:background="#264745" 
    android:src="@mipmap/ic_download" /> 

</RelativeLayout> 
+0

ない答え、ちょうどヒント:これは**誤用される** ' android:src = "@mipmap/ic_download" ' - mipmaps'フォルダには** app laucheのみが含まれていますrアイコン**。 –

答えて

1

追加:

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)

+0

回答をお寄せいただきありがとうございます。私はコードを追加し、pdfはダウンロードされましたが、/storage/emulated/0/data/com.example.spmsejarahscore/files/Documents/THE PDFに保存されています。スマートフォンのドキュメントディレクトリに変更するために何ができるのか分かりますか? – Foong

+1

@Foong 'request.setDestinationInExternalPublicDir("/AppName "、" file_name.extension ");'はカスタムディレクトリに保存します。 –

+0

ありがとう、これは私をたくさん保存する – Foong

関連する問題