In Xamarin.Forms project, I\'m trying to download the file from the blob storage, also I need to save it in a device local folder and need to open it immediately.
I\
You can't expose the File outside of your app in new versions. You should setup couple of things to do that. Follow these steps:
Step 1: Goto your android's manifest file and define file providers inside application tags.
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider" android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
Step 2: Define the Provider path in XML
Step 3: Define the Provider file path in Provider Path XML file you created in Step 2
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="files" path="."/>
<internal-path name="files" path="."/>
<files-path name="files" path="."/>
</paths>
Step 4: When you download your file you should save them in location that you defined in Step 3. I save the file in android at root directory below:
root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Step 5: Opening the File, Following is the method that I'm using to share. Hope you understand and make changes on yours accordingly :
public Task Share(string title, string message, string filePath)
{
var extension = filePath.Substring(filePath.LastIndexOf(".",StringComparison.InvariantCultureIgnoreCase) + 1).ToLower();
var contentType = string.Empty;
Java.IO.File file=new Java.IO.File(filePath);
var apkURI = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName+ ".provider", file);
switch (extension)
{
case "pdf":
contentType = "application/pdf";
break;
case "png":
contentType = "image/png";
break;
default:
contentType = "application/octetstream";
break;
}
var intent = new Intent(Intent.ActionSend);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
intent.SetType(contentType);
intent.PutExtra(Intent.ExtraStream, apkURI);
intent.PutExtra(Intent.ExtraText, string.Empty);
intent.PutExtra(Intent.ExtraSubject, message ?? string.Empty);
var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
chooserIntent.SetFlags(ActivityFlags.ClearTop);
chooserIntent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(chooserIntent);
return Task.FromResult(true);
}
Let me know if you need any help.