簡體   English   中英

將目錄,子目錄和文件復制到SD卡-Android

[英]Copy directory ,sub directories and files to sd card - android

我使用以下代碼將特定目錄及其內容復制到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)

有人可以讓我知道導致此問題的原因和解決方案。

參考: 如何將文件從“資產”文件夾復制到sdcard?

編輯

我可以使用有關許可的技巧之一來解決該異常,該技巧已在此處發布為一個答案。

現在我遇到另一個問題,如下所示:

以下日志顯示我無法在以下路徑中創建文件夾:

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

我不想將數據存儲在/ mnt / sdcard / Android / data / raw內,但是,我希望將資產內部的原始文件夾的內容復制到路徑/ mnt / sdcard / Android / data我從給定的參考鏈接中使用的那段代碼沒有發生這種情況。 任何明顯的原因可能會導致我給出的代碼出現此問題?

您可能忘記了在清單中設置權限

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

暫無
暫無

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

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