简体   繁体   English

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

[英]How to rename downloaded file in android

  1. I'm creating file using Async task the file name is "Book" as my below code 我正在使用异步任务创建文件,文件名为“ Book”,如下所示
  2. When I'm downloading data on that time, I'm creating a file named "Book" once the download finishes. 当我当时下载数据时,一旦下载完成,便会创建一个名为“ Book”的文件。 Now how do I change the file name "Book" into some other name in onPostExecute method? 现在如何在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(); } } 

new File(“ / Book” + mBookDetails.getBookId())。renameTo(new File(“ / BookDownload” + mBookDetails.getBookId())

use this working example to rename the File 使用此工作示例重命名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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM