【问题标题】:In Android Studio Pdf/.doc file is corrupted after downloading it from Download Manager在 Android Studio Pdf/.doc 文件从下载管理器下载后损坏
【发布时间】:2017-08-18 09:40:29
【问题描述】:

1) 通过下载管理器在 android studio 中下载 .docx/pdf 文件后,它会显示一条消息“文件中的内容不可读。你想恢复它吗?”
2) 但是当我下载 jpg 或 mp3 时,它工作正常。

我的代码:

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;
}

【问题讨论】:

  • 你得到这个问题的答案了吗?

标签: android pdf android-download-manager download-manager


【解决方案1】:

公共类 Puneuniversity_mechanical_Engineering_SE 扩展 AppCompatActivity { // 定义您自己的广告单元 ID 后删除下面的行。 私有静态按钮 Btn_Download; 下载管理器下载管理器;

@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);

                }
            }
    );


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多