将pdf从下载移动到外部存储文件夹

时间:2017-04-02 22:02:29

标签: android pdf fileoutputstream

我需要移动并重命名pdf文件。

我正在使用文件选择器来选择似乎有效的pdf:

File selected = new File(fileList.get(position));    
String myFileString = selected.toString();

这为我提供了一个现有文件的字符串,例如:" /storage/emulated/0/Download/myPdf.pdf"

下一步是尝试将文件重命名并复制到我设置的名为tsp的文件夹中:

String myRenamedPdf= "myRenamedPdf.pdf";

File fromLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + myFileString);
File toLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tsp/" + MyRenamedPdf);

    FileInputStream inStream = new FileInputStream(fromLocation);
    FileOutputStream outStream = new FileOutputStream(toLocation);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();
    outStream.close();

当我检查时,新文件不存在。有人可以帮帮我吗?

0 个答案:

没有答案