从原始文件复制到外部(辅助)SD卡

时间:2016-06-27 05:24:50

标签: java android io

我知道很多问题已经得到解答, 但在我的情况下,我的代码在Oppo,三星手机上正常工作但在MI,MOto G,Lenavo手机上无法正常工作

这是我的代码:

辅助SD卡的字符串返回路径:

public static String getExternalStorage() {
        File rootFolder = new File("/");
        boolean isSdcardRemovable = false;
        String path = null;
     /* loop: */
     for (int i = 0; i < rootFolder.listFiles().length; i++) {

        if (rootFolder.listFiles()[i].listFiles() != null
                && !rootFolder.listFiles()[i].toString().contains("system")
                && !rootFolder.listFiles()[i].toString().contains("etc")
                && !rootFolder.listFiles()[i].toString().contains("dev")) {

            File dataDir = new File(Environment.getDataDirectory()
                    .getAbsolutePath());

            long dataDirSize = dataDir.getFreeSpace() / (1000 * 1000);
            long folderSize = rootFolder.listFiles()[i].getFreeSpace()
                    / (1000 * 1000);

            if (dataDirSize == folderSize
                    || (dataDirSize > folderSize && folderSize > (dataDirSize - 80))) {
                System.err
                        .println("INTERNAL1 " + rootFolder.listFiles()[i]);
                System.err.println(dataDirSize);
                System.err.println(folderSize);
            } else {

                File rootSubFolder1 = new File(
                        rootFolder.listFiles()[i].getAbsolutePath());

                if (rootSubFolder1.listFiles() != null) {

                    for (int j = 0; j < rootSubFolder1.listFiles().length; j++) {

                        if (rootSubFolder1.listFiles()[j].getTotalSpace() != 0
                                && rootSubFolder1.listFiles()[j]
                                        .getFreeSpace() != 0
                                && rootSubFolder1.listFiles()[j]
                                        .listFiles() != null) {

                            Debug.i("fromGetExternalStorage", ""
                                    + rootSubFolder1.listFiles()[j]);

                            if (rootSubFolder1.listFiles()[j].toString()
                                    .contains("sdcard")
                                    || rootSubFolder1.listFiles()[j]
                                            .toString().contains("storage")
                                    || rootSubFolder1.listFiles()[j]
                                            .toString().contains("mnt")) {

                                folderSize = rootSubFolder1.listFiles()[j]
                                        .getFreeSpace() / (1000 * 1000);

                                if (dataDirSize == folderSize
                                        || (dataDirSize > folderSize && folderSize > (dataDirSize - 80))) {
                                    System.err
                                            .println("INTERNAL2 "
                                                    + rootSubFolder1
                                                            .listFiles()[j]);
                                    System.err.println(dataDirSize);
                                    System.err.println(folderSize);
                                } else {
                                    int pos = rootSubFolder1.listFiles()[j]
                                            .getAbsolutePath().lastIndexOf(
                                                    '/');
                                    String str = rootSubFolder1.listFiles()[j]
                                            .getAbsolutePath().substring(
                                                    pos + 1);

                                    if (str.matches("(sd|ext|3039|m_external_sd).*")) {

                                        isSdcardRemovable = true;
                                        System.err.println("EXTERNAL "
                                                + rootSubFolder1
                                                        .listFiles()[j]);
                                        System.err.println(dataDirSize);
                                        System.err.println(folderSize);
                                        path = rootSubFolder1.listFiles()[j]
                                                .getAbsolutePath() + "/";
                                        break loop;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (isSdcardRemovable) {
        if (path != null) {
            Debug.i("new Path from getExternal Storage", path);
        } else {
            Debug.i("fail", "External memory not found.");
        }
    } else {
        Debug.i("fail", "External memory not available.");

    }

    return path;
}` 

此代码我使用路径和复制文件:

  

OutputStream OS = new FileOutputStream(path + File.separator +“name.txt”);

1 个答案:

答案 0 :(得分:0)

首先制作目录

 File wallpaperDirectory = new File("sdcard/Youpath/");
 // have the object build the directory structure, if needed.
        if(!wallpaperDirectory.exists()) {
            wallpaperDirectory.mkdirs();
        }

要复制的代码

final int[] mList= new int[] { R.raw.a, R.raw.b, R.raw.c,R.raw.d,R.raw.e,R.raw.f,
                R.raw.g,R.raw.h,R.raw.i,R.raw.j,R.raw.k,R.raw.l,R.raw.m,R.raw.n,R.raw.o,R.raw.p,R.raw.q
        ,R.raw.r,R.raw.s,R.raw.t,R.raw.u};
        for (int i = 0; i < mList.length; i++) {
            try {
                String path = "sdcard/Youpath/";
                File dir = new File(path);
                if (dir.mkdirs() || dir.isDirectory()) {
                    String mName= "YourSetName"+ String.valueOf(i+1) + ".extension";
                    CopyRAWtoSDCard(mList[i], path + File.separator + mName);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

CopyRAWtoSDCard功能

   private void CopyRAWtoSDCard(int id, String path) throws IOException {
    InputStream in = getResources().openRawResource(id);
    FileOutputStream out = new FileOutputStream(path);
    byte[] buff = new byte[1024];
    int read = 0;
    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
    } finally {
        in.close();
        out.close();
    }
}