簡體   English   中英

將文件從原始文件復制到外部(備用)SD卡

[英]File copy from raw to External (secondary) SD card

我知道已經回答了許多問題, 但是就我而言,我的代碼在Oppo,三星手機上正常運行,但在MI,MOto G,Lenavo手機上卻無法正常工作


這是我的代碼:

備用SD卡的String返回路徑:

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 =新的FileOutputStream(path + File.separator +“ name.txt”);

首先制作目錄

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM