简体   繁体   中英

File copy from raw to External (secondary) SD card

I know that many question has been answered, but in my case my code is working properly on Oppo, samsung phone but not work on MI, MOto G, Lenavo phone


Here is my code:

This String return path of secondary SD card:

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;
}` 

And This Code that I use path and copy file:

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

First Make Directories

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

Code to Copy

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 Function

   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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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