如何让Android文件的代码在SD卡中移动?

时间:2014-10-11 04:30:18

标签: android

我将制作root-Tool。但我需要文件移动源请告诉我它(代码)?... 我很抱歉,请回答我。

**`" How to make the Code for Android File Moving in the SDcard "`**

1 个答案:

答案 0 :(得分:0)

要将文件从一个位置移动到另一个位置,您可以使用:

private void moveFile(String inputPath, String outputPath) {

    System.out.println(inputPath);
    System.out.println(outputPath);



    InputStream in = null;
    OutputStream out = null;
    try {

        // create output directory if it doesn't exist
        File dir = new File(outputPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        in = new FileInputStream(inputPath);
        out = new FileOutputStream(outputPath);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

        // write the output file
        out.flush();
        out.close();
        out = null;

        // delete the original file
        new File(inputPath).delete();

    }catch (FileNotFoundException fnfe1) {
        Log.e("File not found exception", fnfe1.getMessage());
    } catch (Exception e) {
        Log.e("Other Exception", e.getMessage());
    }

}

要调用此方法,您可以执行以下操作:

moveFile(Environment.getExternalStorageDirectory() + "/download/abc.txt", Environment.getExternalStorageDirectory() + "/abc.txt");

此外,您还需要在AndroidManifest.xml

中提供书面许可
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>