获取绝对路径并移动文件

时间:2014-10-12 06:43:02

标签: android file move absolute-path

我是Android编程新手。我正在创建一个应用程序,我应该从Custom Gallery获取文件的绝对路径并将该文件移动到私人文件夹。我搜索了很多,但我无法得到任何完美的答案。请帮我编写代码。感谢。

1 个答案:

答案 0 :(得分:0)

获取SD卡的绝对路径:

String SDCARD_PATH = Environment.getExternalStorageDirectory().getPath() + "/";


File dirFolder = new File(path);

File[] folders = dirFolder.listFiles(); // list of all files and folder into this path
for (File file : folders) {
    if (file.isDirectory()) {
        file.getName() // this will return folder name
    } else {
        file.getName(); // this will return file name with extension
    }
}

在整个过程中执行递归方法。

复制文件:

private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        InputStream myInput = new FileInputStream(filePath + fileNameWithExtension);

        // Path to the just created empty db
        String outFileName = OUTPUT_PATH + "/" + FILE_NAME;

        // Open the empty db as the output stream
        new File(outFileName).createNewFile();
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

之后只需删除文件:

String myFile = filePath+"/"+fileNameWithExtension;
if (new File(myFile).delete())
            // do something
        else
            // do something
相关问题