资产中的复制文件失败

时间:2015-03-28 01:08:10

标签: android xamarin

我正在尝试将资产中的二进制文件复制到/ data / data / program / files / 我的代码编译但崩溃,拒绝复制文件的权限。

   using System;
   using Android.App;
   using Android.Content;
   using Android.Runtime;
   using Android.Views;
   using Android.Widget;
   using Android.OS;
   using System.IO;

namespace com.jocala.test
{
    [Activity (Label = "com.jocala.test", MainLauncher = true, Icon =   "@drawable/icon")]
    public class MainActivity : Activity
    {
        string filename = "busybox";
         string filepath = System.Environment.GetFolderPath  (System.Environment.SpecialFolder.Personal);


        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

        // Set our view from the "main" layout resource

        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it

        Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("Installing");
                using (Stream stream = Assets.Open (filename)) 
                {
                 using(var fileStream = System.IO.File.Create (filepath))
                    {
                    stream.CopyTo(fileStream );
                    }
                  }

              };
            }
         }
     }

[mono-rt] [ERROR]致命不受限制的异常:System.UnauthorizedAccessException:拒绝访问路径'/data/data/com.jocala.test/files'。

1 个答案:

答案 0 :(得分:-1)

private void copyAssetsFiles() {

        // FileUtils.deleteRootDir(FileUtils.getRootFile(SplashScreen.this));
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
            files = assetManager.list("monoco");// here your folder name of assets folder.
        } catch (IOException e) {
            Log.e("tag", "Failed to get asset file list.", e);
        }
        for (String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open("monoco/" + filename);
                File outFile = FileUtils.createNewFile(SplashScreen.this,
                        filename);

                if (outFile != null) {

                    out = new FileOutputStream(outFile);
                    copyFile(in, out);

                    out.flush();
                    out.close();
                    out = null;

                } else
                    Log.d("asd", "Files allready exist..." + filename);

                in.close();
                in = null;

            } catch (IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            }

        }
    }

    // ============================================//
    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        Log.d("asd", "Copies file");
    }

在清单文件中提供写入权限:

相关问题