Xamarin Android Download PDF/XLS/DOCX

KhaiFMsia 71 Reputation points
2022-02-11T01:50:24.687+00:00

Helo2,
Im looking for solution to download files (ie: pdf, docx, xls,etc.) using content resolver like in example provided on below link (by @JarvanZhang ) but no luck.
https://learn.microsoft.com/en-us/answers/questions/531801/save-a-picture-from-camera-to-device-gallery-using.html
Is there any additional part to add?
Here's my code

Android.Content.Context context = MainActivity.Instance;
Android.Content.ContentResolver resolver = context.ContentResolver;
Android.Content.ContentValues contentValues = new Android.Content.ContentValues();
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.DisplayName, Path.GetFileName(url));
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.MimeType, "application/pdf, application/docx");
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.RelativePath, "Download" + "/" + folder);
Android.Net.Uri fileUri = resolver.Insert(Android.Provider.MediaStore.Downloads.ExternalContentUri, contentValues);
var os = resolver.OpenOutputStream(fileUri);
var webClient = new WebClient();
byte[] byteFile = webClient.DownloadData(url);
MemoryStream stream = new MemoryStream(byteFile);
int length;
byte[] buffer = new byte[4096];
while ((length = stream.Read(buffer)) > 0)
{
os.Write(buffer, 0, length);
}
os.Flush();
os.Close();

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2022-02-14T02:44:43.36+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I created a basic demo to test the function with the following code, it works as expected. The only difference is MimeType, please set the type for the file separately. For example, set MimeType to application/pdf if you want to save a pdf file. You could use switch to detect the file type.

       if (Android.OS.Build.VERSION.SdkInt > BuildVersionCodes.Q)  
       {  
           ContentResolver resolver = context.ContentResolver;  
           ContentValues contentValues = new ContentValues();  
           contentValues.Put(MediaStore.MediaColumns.DisplayName, fileName);  
           contentValues.Put(MediaStore.MediaColumns.MimeType, "image/png");  
           contentValues.Put(MediaStore.MediaColumns.RelativePath, "Download/" + "test");  
           Android.Net.Uri imageUri = resolver.Insert(MediaStore.Downloads.ExternalContentUri, contentValues);  
           var os = resolver.OpenOutputStream(imageUri);  
           BitmapFactory.Options options = new BitmapFactory.Options();  
           options.InJustDecodeBounds = true;  
           var bitmap = BitmapFactory.DecodeStream(stream);  
           bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);  
           os.Flush();  
           os.Close();  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.