Copy file from assets to sdcard

How can I copy file from assets to some path on sdcard? Code snippet would be useful.

Posts

  • StevenTheEvenStevenTheEven SEMember ✭✭✭

    Have you searched this forums to see if someone else have asked the same thing before you?

  • alberto.palberto.p ITMember ✭✭

    using (var asset = context.Resources.Assets.Open ())
    using (var dest = File.Create ())
    asset.CopyTo (dest);

  • RomanTheRomanThe RUMember

    Got FileNotFound exception when tried to open asset.
    I am sure file that I tried to open exists in Assets folder.

  • alberto.palberto.p ITMember ✭✭

    @RomanThe check if files exists with Assets.List()

  • RomanTheRomanThe RUMember

    Ok, thank you. It's working now.
    Originally my intent was to copy .apk file from assets and install it programmatically. So, no I have following code:
    var tmpPath = Android.OS.Environment.ExternalStorageDirectory.Path + "/tmp_app.apk"; using (var asset = Assets.Open("Test/Cnd.apk")) using (var dest = File.Create (tmpPath)) asset.CopyTo (dest); Intent intent = new Intent(Intent.ActionView); intent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(tmpPath))); intent.SetType("application/vnd.android.package-archive"); StartActivity(intent);

    But it throws ActivityNotFoundException when I tried to run it at emulator. I also see NULL in intent.Data field during debugging. Can I make it work somehow?

  • MahdiZareiMahdiZarei USMember ✭✭
    edited July 2015

    I am using below codes but when debugging, it throw an error with this content :
    > System.UnauthorizedAccessException: Access to the path "/storage/sdcard/swfa.apk" is denied.
    var tmpPath = Android.OS.Environment.ExternalStorageDirectory.Path + "/swfa.apk"; using (var asset = Assets.Open("swf.apk")) using (var dest = System.IO.File.Create(tmpPath)) asset.CopyTo(dest); Intent setupIntent = new Intent(Intent.ActionView); setupIntent.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(tmpPath)), "application/vnd.android.package-archive"); StartActivity(setupIntent);
    In addition, I used all permission that needed !

Sign In or Register to comment.