2017-08-18 48 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); 

       } 
      } 
    ); 


}