2017-08-18 149 views
0

1)通過下載管理器在android studio中下載.docx/pdf文件後,它顯示一條消息「文件中的不可讀內容。是否想要恢復它?「
2)但是,當我下載一個JPG或MP3,它工作正常。在Android Studio Pdf/.doc文件從Download Manager下載後損壞

我的代碼:

private long DownloadData (Uri uri, View v) { 

    long downloadReference; 

    downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); 
    DownloadManager.Request request = new DownloadManager.Request(uri); 

    //Setting title of request 
    request.setTitle("ZoraizCV"); 
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); 
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

    //Setting description of request 
    request.setDescription("Android Data download using DownloadManager."); 

    //Set the local destination for the downloaded file to a path within the application's external files directory 
    if(v.getId() == R.id.DownloadMusic) 
     request.setDestinationInExternalFilesDir(MainActivity.this, Environment.DIRECTORY_DOWNLOADS,"zoraizCV.docx"); 


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



    Button DownloadStatus = (Button) findViewById(R.id.DownloadStatus); 
    DownloadStatus.setEnabled(true); 
    Button CancelDownload = (Button) findViewById(R.id.CancelDownload); 
    CancelDownload.setEnabled(true); 

    return downloadReference; 
} 

回答

0

公共類Puneuniversity_mechanical_Engineering_SE擴展AppCompatActivity {// 定義自己的廣告單元ID後刪除以下行。 私人靜態按鈕Btn_Download; DownloadManager downloadManager;

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

    // Load an ad into the AdMob banner view. 
    AdView adView = (AdView) findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder() 
      .setRequestAgent("android_studio:ad_template").build(); 
    adView.loadAd(adRequest); 
    Btn_Download=(Button)findViewById(R.id.Download); 
    Btn_Download.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
        Uri uri= Uri.parse("https://drive.google.com/uc?export=download&id=0B6C5vNWHm0sCSVlXZ3ljTnA3Tjg"); 
        DownloadManager.Request request=new DownloadManager.Request(uri); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
        Long reference=downloadManager.enqueue(request); 

       } 
      } 
    ); 


}