【问题标题】:Copying raw file into SDCard?将原始文件复制到 SDCard 中?
【发布时间】:2012-01-29 16:06:35
【问题描述】:

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

我该怎么做?有人指导我吗?

【问题讨论】:

标签: android android-sdcard file-copying


【解决方案1】:

从资源中读取,写入 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();
}

【讨论】:

  • 我如何获得复制的字节数,就像我们可以在从互联网下载文件时发布进度一样。
  • @newBie 每次通过 while 循环它都会复制 1024 个字节
  • 感谢您的解决方案!你如何决定缓冲区的大小为 1024?
  • 这是 Java 中的一个常见习语,1024 是一个很好的整数 :)。可以根据您的资源进行调整,但可能操作系统已经足够智能,可以在您点击文件后缓存文件,因此除非资源非常巨大,否则可能没有可衡量的差异。
【解决方案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();
    }

}

【讨论】:

    【解决方案3】:

    Environment.getExternalStorageDirectory() 这个方法是 在 API 级别 29 中已弃用,并且不允许创建 给定路径上的目录/文件。

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

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

    文件管理器--> 存储--> Android --> 数据 --> com.yourapp.packagename --> 文件 --> FOLDER_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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 2014-01-30
      • 2018-08-26
      相关资源
      最近更新 更多