将原始文件复制到SDCard?

时间:2011-12-29 05:53:24

标签: android android-sdcard file-copying

我的res/raw文件夹中有一些音频文件。出于某些原因,我想将这些文件复制到我的SDCard当我的应用程序启动时。

我怎么能这样做?有人指导我吗?

3 个答案:

答案 0 :(得分:43)

从资源中读取,写入SD卡上的文件:

InputStream in = getResources().openRawResource(R.raw.myresource);
FileOutputStream out = new FileOutputStream(somePathOnSdCard);
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();
}

答案 1 :(得分:2)

将文件从原始存储复制到外部存储:

这是我用来完成这项工作的方法,此方法接收资源ID和存储所需的名称,例如:

copyFiletoExternalStorage(R.raw.mysound, "jorgesys_sound.mp3");

方法:

private void copyFiletoExternalStorage(int resourceId, String resourceName){
    String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
    try{
        InputStream in = getResources().openRawResource(resourceId);
        FileOutputStream out = null;
        out = new FileOutputStream(pathSDCard);
        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();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

答案 2 :(得分:1)

环境。 getExternalStorageDirectory() API级别29中已弃用,将不允许创建 给定路径上的目录/文件。

以下代码可用于创建目录并从/ res / raw目录下载文件。

如果您的设备具有 Android 10(API级别29)或更高版本,它将 在以下位置下载文件

文件管理器->存储-> Android->数据-> com.yourapp.packagename->文件-> FOLDER_NAME -> file_name.png

private void downloadFileFromRawFolder(){
try {
                InputStream in = getResources().openRawResource(
                        getResources().getIdentifier("file_name",
                                "raw", getPackageName())); // use only file name here, don't use extension
                File fileWithinMyDir = new File(checkFolder(), "file_name.png"); //Getting a file within the dir.
                Log.e("FILEPATH ", "fileWithinMyDir " + fileWithinMyDir);
                FileOutputStream out = new FileOutputStream(fileWithinMyDir);
                byte[] buff = new byte[1024 * 1024 * 2]; //2MB file
                int read = 0;

                try {
                    while ((read = in.read(buff)) > 0) {
                        out.write(buff, 0, read);
                    }
                } finally {
                    in.close();
                    out.close();
                }
                Log.d(TAG, "Download Done ");
            } catch (IOException e) {
                Log.e(TAG, "Download Failed " + e.getMessage());
                e.printStackTrace();
            }

}

/ *创建目录* /

    private File checkFolder() {
        String path;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            path = getExternalFilesDir(null).getAbsolutePath() + "FOLDER_NAME";
        } else {
            path = Environment.getExternalStorageDirectory() + "FOLDER_NAME";
        }
        File dir = new File(path);
        boolean isDirectoryCreated = dir.exists();
        if (!isDirectoryCreated) {
            isDirectoryCreated = dir.mkdir();
            Log.d("Folder", "Created = " + isDirectoryCreated);
        }

        Log.d("Folder", "Created ? " + isDirectoryCreated);
        return dir;
    }
}