0

I've found this: How to download image and save it in local storage using Xamarin-Forms.?

This partially adresses my problem except for two points:

  • I'd need to download the image to the gallery, not the apps'path
  • I need this to work for both, android and IOs. This seems to only work for Android.

Basically i know the URL of a file online, and need to download it to the gallery. It would be great if ic ould "save" it from inside the application, instead of "downloading". It would be nice if the client cant figure out the URL of the images he wants to save.

EDIT:

Now I am using FFImageLoading.. here is my current (not working) code..

 private async void SaveToGallery_Clicked(object sender, EventArgs e)
    {
        var img = await MyImage.GetImageAsJpgAsync(quality: 100);

        string fileName = uri.ToString().Split('/').Last();

        DependencyService.Get<IMediaService>().SaveImageFromByte(img, fileName);
    }

Android MediaService.cs

[assembly: Xamarin.Forms.Dependency(typeof(MediaService))]
namespace GalShare.Droid
{
public class MediaService : IMediaService
{
    Context CurrentContext => CrossCurrentActivity.Current.Activity;
    public void SaveImageFromByte(byte[] imageByte, string fileName)
    {
        try
        {

            Java.IO.File storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
            string path = System.IO.Path.Combine(storagePath.ToString(), fileName);
            System.IO.File.WriteAllBytes(path, imageByte);
            var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
            CurrentContext.SendBroadcast(mediaScanIntent);
        }
        catch (Exception ex)
        {

        }
    }
}
}

Android MainActivity.cs:

namespace GalShare.Droid
{
[Activity(Label = "GalShare", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public int STORAGE_PERMISSION_CODE = 101;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        FFImageLoading.Forms.Platform.CachedImageRenderer.Init(enableFastRenderer: false);

        base.OnCreate(savedInstanceState);

        Forms.SetFlags("CollectionView_Experimental");

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        CachedImageRenderer.InitImageViewHandler();

        string fileName = "galleries.db3";
        string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string completePath = Path.Combine(folderPath, fileName);

        checkPermission("android.permission.write_external_storage", STORAGE_PERMISSION_CODE);

        LoadApplication(new App(completePath));
    }


    public void checkPermission(String permission, int requestCode)
    {
        var thisActivity = Android.App.Application.Context as Activity;
        // Checking if permission is not granted 
        if (ContextCompat.CheckSelfPermission(
                Android.App.Application.Context,
                permission)
            == Android.Content.PM.Permission.Denied)
        {
            RequestPermissions(new String[] { Manifest.Permission.WriteExternalStorage }, requestCode);

        }
        else
        {

        }

    }

}
}
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97
  • downloading a file is the same as in any c# application - use HttpClient to get the bytes, then write to file using Sytem.IO.File. There are numerous existing questions that address this. Saving to the gallery will vary between iOS and Android, but there are MULTIPLE existing questions on SO that explain how to do it. – Jason Mar 20 '20 at 20:58
  • @Jason thanks.. I've been looking around and found a lot of topics, but none of them brought me to a solution. I updated the topic with the code I have so far. but when I click nothign happens, Also, the path somehow does not corrispond to the gallery.. – sharkyenergy Mar 21 '20 at 11:29
  • "not working" - what does this mean? Does it have a compile error, or does it crash at runtime with an exception? Does it doing (or not doing) that it shouldn't? As I explained before, saving to the gallery will be a platform specific operation. Have you looked at other questions that address this? – Jason Mar 21 '20 at 13:35
  • @Jason, I made some huge changes, implemented the permission request, and am working on the andoid side. Currently nothing happens.. the file simply does not get saved.. Updating the Post with the current code – sharkyenergy Mar 21 '20 at 13:39
  • seems like this is null: ` Context CurrentContext => CrossCurrentActivity.Current.Activity;` inside the mediaservice.cs – sharkyenergy Mar 21 '20 at 13:52

1 Answers1

0

Initialilzing CrossCurrentActivity in the MainActivity.cs solved the problem:

CrossCurrentActivity.Current.Init(this, bundle);
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97