locked
How to download and save file into download folder RRS feed

  • Question

  • User47522 posted

    Hi, I'm trying to download file and save into download folder via this code, but I'm getting exception:

    ``` public void Download(string uri, string filename) { try { Android.Net.Uri contentUri = Android.Net.Uri.Parse(uri); Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(contentUri); r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename); r.AllowScanningByMediaScanner(); r.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted); Android.App.DownloadManager dm = (Android.App.DownloadManager)Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.DownloadService);

                dm.Enqueue(r);
            }
            catch(Exception ex) { ex.Exception("Download");
    
        }
    

    I set this permission in AndroidManifest ```

    exception: ``` [0:] Download exception: No permission to write to /storage/emulated/0/Download/Chris.Brown-Questions1080.mp4: Neither user 10189 nor current process has android.permission.WRITEEXTERNALSTORAGE. Trance: at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 at Java.Interop.JniEnvironment+InstanceMethods.CallLongMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in :0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt64Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in :0 at Android.App.DownloadManager.Enqueue (Android.App.DownloadManager+Request request) [0x00031] in :0 at Soorena.Views.MusicPage.Download (System.String uri, System.String filename) [0x00041] in F:\MyApp\MyApp\Views\MusicPage.xaml.cs:216 --- End of managed Java.Lang.SecurityException stack trace --- java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/Chris.Brown-Questions1080.mp4: Neither user 10189 nor current process has android.permission.WRITEEXTERNALSTORAGE. at android.os.Parcel.readException(Parcel.java:1665) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) at android.content.ContentResolver.insert(ContentResolver.java:1291) at android.app.DownloadManager.enqueue(DownloadManager.java:1015) at mono.java.lang.RunnableImplementor.n_run(Native Method) at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30) at android.os.Handler.handleCallback(Handler.java:761) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

    Source: mscorlib ```

    How can I download file in Downloads folder? where I'm doing wrong?

    Thanks

    Thursday, August 17, 2017 9:20 PM

Answers

  • User47522 posted

    I find out how to solve this. in Api 23 or later you have to check permission. for this I use this plugin PluginPermissions by @JamesMontemagno and it works well.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, August 17, 2017 9:54 PM

All replies

  • User47522 posted

    I find out how to solve this. in Api 23 or later you have to check permission. for this I use this plugin PluginPermissions by @JamesMontemagno and it works well.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, August 17, 2017 9:54 PM
  • User290865 posted

    here is a very good solution. https://www.youtube.com/watch?v=rcbm6R4e8jQ&t=243s

    Monday, March 19, 2018 7:13 PM
  • User355323 posted

    @Rohit_Arnav Thanks Rohit its working as expected

    Thursday, May 14, 2020 9:38 AM