在Android Studio中,从Download Manager下载后,Pdf / .doc文件已损坏

时间:2017-08-18 09:40:29

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

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

1 个答案:

答案 0 :(得分:0)

public class Puneuniversity_mechanical_Engineering_SE扩展AppCompatActivity {     //定义自己的广告单元ID后,删除以下行。     private static Button Btn_Download;     DownloadManager downloadManager;

public void clean(FileWriter fileWriter) throws IOException{
        fileWriter.append("\n");
        fileWriter.flush();
        fileWriter.close();
    }

public static long create(){
        long value;
        value = System.currentTimeMillis();
        return value;
    }

    public static void main(String[] args) throws Exception {
        long value = create();
        File filePath = new File ("G:/Mirriad/test_"+value+".csv");
        String Fileheader="UID,TITLE,ACTOR,DIRECTOR,COUNTRY,FORMAT,BITRATE,FRAMES_PER_SHIFT,ASPECT,WIDTH,PATH";
        FileWriter fileWriter = new FileWriter(filePath, true);
        fileWriter.append(Fileheader.toString());

        MergeCode1 fw = new MergeCode1();
        String path = "G:/Mirriad/ScriptTest";

        fw.create();
        System.out.println(fw.create());
        fw.walk(path, filePath, fileWriter);
        fw.clean(fileWriter);   
    }