开发者

MonoDroid: Copying an Asset to SD Card

开发者 https://www.devze.com 2023-03-27 19:37 出处:网络
Using MonoDroid in Visual Studio and an emulator, I am trying to copy a asset, \"db.sqlite\" from the assets folder to the SD card so that I can open the database for read/write.

Using MonoDroid in Visual Studio and an emulator, I am trying to copy a asset, "db.sqlite" from the assets folder to the SD card so that I can open the database for read/write.

When I run the app, it dies. MonoDroid is not giving me any debug info.

string d开发者_StackOverflow中文版estPath = Path.Combine(Environment.GetFolderPath(
       Environment.SpecialFolder.Personal), "db.sqlite");

if (!File.Exists(destPath))
 using (Stream stream = Assets.Open("db.sqlite"))
 {
   stream.CopyTo(File.Create(destPath));
   stream.Close();
 }


Check the Android Debug Log:

http://android.xamarin.com/Documentation/Guides/Android_Debug_Log


Try saving the file with database file .mp3 extension and then use the following script to copy it to SD card

 private void copyFilesToSdCard() {
        copyFileOrDir(""); // copy all files in assets folder in my project
    }

    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() && !path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                    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 + "/";

                    if (!path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                        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(".mp3")) // 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());
        }


Code that worked for me (it copies recursively file or directory and all subdirectories ):

 private void copy(string SourcePath){
        string[] list = Assets.List(SourcePath);
        if (list.Length > 0) {
            Directory.CreateDirectory(AndroidResorces.GetDatabaseStorage() + "/" + SourcePath);
            foreach (string dirPath in Assets.List(SourcePath)){
                string newPath = SourcePath + "/" + dirPath;
                copy(newPath);
            }
        }
        else{
            Assets.Open(SourcePath).CopyTo(new FileStream(AndroidResorces.GetDatabaseStorage() + "/" + SourcePath, FileMode.OpenOrCreate));
        }
    }

Usage: copy(path_relative_to_assets_root);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号