如何在Android中重命名下载的文件

时间:2018-12-21 09:02:52

标签: java android

  1. 我正在使用异步任务创建文件,其文件名为“ Book”,如下所示
  2. 当我当时下载数据时,一旦下载完成,我将创建一个名为“ Book”的文件。现在如何在onPostExecute方法中将文件名“ Book”更改为其他名称?

public class MyDownloaderAsyncTask extends AsyncTask<String, Void, Boolean>  {

    private DownloadManager.DownloadCompletedListner mListner;
    public BookProperties mBookDetails;

    public MyDownloaderAsyncTask(DownloadManager.DownloadCompletedListner listener, BookProperties bookdetails) {
        mListner = listener;
        mBookDetails = bookdetails;

    }

    @Override
    protected Boolean doInBackground(String... strings) {

             Log.i(TAG, "Success Download canceled 1111");
             String tMainFolder = String.valueOf(BaseApplication.getInstance().getAppContext().getDir("MonnFamily", Context.MODE_PRIVATE));
             tMainFolder += "/Book" + mBookDetails.getBookId();  //file which download name
             Log.i(TAG, "Assynctask Ma" + tMainFolder);
             Log.i(TAG, "book id and book name" + mBookDetails.getBookId() + mBookDetails.getBookName());
             downloadBookDetails(tMainFolder, ContentfulConstants.BOOK_MAIN_IMAGE + ".png", mBookDetails.getBookMainImage());
             downloadBookDetails(tMainFolder, ContentfulConstants.BOOK_MAIN_AUDIO + ".mp3", mBookDetails.getBookSound());

             for (PageDetailProperties pageDetails : mBookDetails.getPageDetail()) {

                 String tPageNumber = pageDetails.getPageNumber().toString();
                 downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_IMAGE + tPageNumber + ".png", pageDetails.getPageImage());
                 downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_AUDIO + tPageNumber + ".mp3", pageDetails.getPageAudio());
                 downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_TEXT + tPageNumber + ".txt", pageDetails.getPageText());

             }

        return true;

    }




    protected void onPostExecute(Boolean result) {
        Log.i(TAG, "Execute Download befor");
        //This is run on the UI thread so you can do as you wish here
        if (result) {
            Log.i(TAG, "Success Download");
            ((LibraryView) BaseApplication.getInstance().getCurrentActivity()).hideActivityView();
            mListner.downloadCompleted();
           // mBookDetails = pBook;


//          //  mBookDetails = pBook;
//            String  tMainFolder = String.valueOf(BaseApplication.getInstance().getAppContext().getDir("MonnFamily", Context.MODE_PRIVATE));
//            Log.i(TAG, "Book downloadBookData" + tMainFolder);
//            tMainFolder += "/Book" + mBookDetails.getBookId();
//            tMainFolder +="/BookDownload" + mBookDetails.getBookId();
//            Log.i(TAG, "Post Assync in DownloadManager" + tMainFolder);
//
//            File directory = new File(tMainFolder);
//            File directory1 = new File(tMainFolder);
//            if (directory.exists()) {
//                directory.renameTo(directory1);
//                Log.i(TAG, "Download Manager Directory" + directory1);
//            }

        }
    }




    private void downloadBookDetails(String pMainFolder, String pFileName, String pDownloadURL) {
        Log.i(TAG, "Coming to this downloadBookDetails ");
        try {
            URL url = new URL(pDownloadURL);
            Log.i(TAG, "pDownload URL"+  url);
            URLConnection ucon = url.openConnection();
            ucon.setReadTimeout(5000);
            ucon.setConnectTimeout(10000);


            InputStream is = ucon.getInputStream();
            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

            File directory = new File(pMainFolder, pFileName);
            Log.i(TAG, "File Name dir" + directory);
            FileOutputStream outStream = new FileOutputStream(directory);
            byte[] buff = new byte[5 * 1024];

            int len;
            while ((len = inStream.read(buff)) != -1) {
                outStream.write(buff, 0, len);

            }
            outStream.flush();
            outStream.close();
            inStream.close();
        } catch (Exception e) {
            //Add Network Error.
            Log.i(TAG, "Download Error Exception " + e.getMessage());

            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:0)

新文件(“ / Book” + mBookDetails.getBookId())。renameTo(新文件(“ / BookDownload” + mBookDetails.getBookId())

答案 1 :(得分:0)

使用此工作示例重命名File

File sdcard=new File(tMainFolder+"/Book/");
File old=new File(sdcard,mBookDetails.getBookId());
File to = new File(sdcard, "renamefile.jpg");
boolean success=old.renameTo(to);