java - 将目录、子目录和文件复制到 SD 卡 - android

标签 java android android-layout filenotfoundexception android-assets

我使用以下代码将特定目录及其内容复制到 SD 卡。该目录位于 res/raw 文件夹中。

以下是我使用的代码:

public class CopyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    copyFilesToSdCard();
}

static String extStorageDirectory =  Environment.getExternalStorageDirectory().toString();
final static String TARGET_BASE_PATH = extStorageDirectory+"/Android/data/";

private void copyFilesToSdCard() {
    copyFileOrDir("");
}

private void copyFileOrDir(String path) {
    AssetManager assetManager = this.getAssets();
    String assets[] = null;
    try {
        Log.i("tag", "copyFileOrDir() "+path);
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            String fullPath =  TARGET_BASE_PATH + path;
            Log.i("tag", "path="+fullPath);
            File dir = new File(fullPath);
            if (!dir.exists())
                if (!dir.mkdirs());
                    Log.i("tag", "could not create dir "+fullPath);
            for (int i = 0; i < assets.length; ++i) {
                String p;
                if (path.equals(""))
                    p = "";
                else 
                    p = path + "/";

                    copyFileOrDir( p + assets[i]);
            }
        }
    } catch (IOException ex) {
        Log.e("tag", "I/O Exception", ex);
    }
}

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    String newFileName = null;
    try {
        Log.i("tag", "copyFile() "+filename);
        in = assetManager.open(filename);
        if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
            newFileName = TARGET_BASE_PATH + filename.substring(0, filename.length()-4);
        else
            newFileName = TARGET_BASE_PATH + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", "Exception in copyFile() of "+newFileName);
        Log.e("tag", "Exception in copyFile() "+e.toString());
    }

}
}

异常:

 Exception in copyFile() of /mnt/sdcard/Android/data/raw/edu/anees.txt
 Exception in copyFile() java.io.FileNotFoundException:
 /mnt/sdcard/Android/data/raw/edu/anees.txt: open failed: ENOENT (No such file or directory)

谁能告诉我导致此问题的原因及其解决方案。

引用:How to copy files from 'assets' folder to sdcard?

编辑

我可以通过在此处作为一个答案发布的有关权限的提示之一解决异常。

现在又遇到一个问题,如下:

以下日志显示我无法在以下路径中创建文件夹:

Log.i("tag", "could not create dir "+fullPath);// fullPath = /mnt/sdcard/Android/data/raw

我不想将数据存储在/mnt/sdcard/Android/data/raw 中,而是希望将 assets 中 raw 文件夹的内容复制到路径/mnt/sdcard/我从我提供的引用链接中使用的代码段没有发生 Android/data 。有什么明显的原因可能导致我提供的代码出现这种情况?

最佳答案

您可能忘记在 list 中设置权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于java - 将目录、子目录和文件复制到 SD 卡 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11080386/

相关文章:

java.security.cert.CertificateNotYetValidException 即使在 "valid from"日期之后

java - 数据流 GCS 到 BigQuery - 如何每个输入输出多行?

java - HTTPS 不适用于域名或静态 IP,但适用于本地 IP

android - System.getenv ("SECONDARY_STORAGE") 使用 AVD 返回 null

android - 如何在 MapView 上进行 3DTransition?

java - 使用反射加载类时避免 ClassNotFoundException

android - 如何在android中从sqlite数据库中插入和获取数据?

android - 约束布局 : How to constrain a View's top to the bottom of the view of largest height?

android - 如何将 Textview 彼此相邻添加,如果它们不合适则将它们移动到下一行

java - 基于 ContentObserver 的应用程序的短信记录器 : how to create AndroidManifest. xml