3

在我的应用程序中,我需要下载一些文件(图像、Pdf 和 txt)。我成功地通过下载文件夹中的下载管理器保存文件,但是我想要如果文件已经存在于下载文件夹中并且用户再次单击相同的文件,然后它会删除下载文件夹中的旧文件并替换为新文件。请让我知道这怎么可能?

我的代码:

  database = new DMS_Database(Files_Folders_Activity.this);
        if(!database.dididExist((doc_Id)))    // if file does not exist then it will save file in the database and in downloads folder.
        {
            database.Save_Doc(doc_Id, name, url);   //Database method
            System.out.println("you are here: Save Files");
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setTitle(name);
            // in order for this if to run, you must use the android 3.2 to compile your app
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
            {
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            }
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/Downloads", name);
            DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
        }
        else
        {
            database.update_Doc(Doc_Id,name,url);    
//But if file already exist then How to delete file and again save on same location. I don't want duplicate file. Right now it is creating two websites also not updating database.
            System.out.println("you are here: update files");
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setTitle(name); 
            // in order for this if to run, you must use the android 3.2 to compile your app
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            {
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            }
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/Downloads", name);
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
        }
                database.close();

请让我现在知道它是可能的,谢谢。

4

0 回答 0