Search code examples
androidfile-ioxamarin.androidbitmapimage

How to save an image to Internal Storage and then show it on another activity?


I am working in Xamarin.Android. I have two activities on which I need to show the same image. On the first screen, I download it from a web URL and show it but I don't want to do the same on second screen. I want to save it to Internal Storage when it gets downloaded on the first screen and then simply retrieve it from there to show on second activity. How can I do that?

Here is my code that I am using on first activity:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    this.SetContentView (Resource.Layout.Main);

    String uriString = this.GetUriString();
    WebClient web = new WebClient ();
    web.DownloadDataCompleted += new DownloadDataCompletedEventHandler(web_DownloadDataCompleted);
    web.DownloadDataAsync (new Uri(uriString));
}

void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    if (e.Error != null)
    {
        RunOnUiThread(() =>
            Toast.MakeText(this, e.Error.Message, ToastLength.Short).Show());
    }
    else
    {
        Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);

        // THIS IS WHERE I NEED TO SAVE THE IMAGE IN INTERNAL STORAGE //

        RunOnUiThread(() =>
            {
                ProgressBar pb = this.FindViewById<ProgressBar> (Resource.Id.custLogoProgressBar);
                pb.Visibility = ViewStates.Gone;

                ImageView imgCustLogo = FindViewById<ImageView>(Resource.Id.imgCustLogo);
                imgCustLogo.SetImageBitmap(bm);
            });
    }
}

Now for saving the image, here is what I did inspired by this:

        Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);

        ContextWrapper cw = new ContextWrapper(this.ApplicationContext);
        File directory = cw.GetDir("imgDir", FileCreationMode.Private);
        File myPath = new File(directory, "test.png");

        FileOutputStream fos = null;
        try 
        {
            fos = new FileOutputStream(myPath);
            bm.Compress(Bitmap.CompressFormat.Png, 100, fos);
            fos.Close();
        }
        catch (Exception ex) 
        {
            System.Console.Write(ex.Message);
        }

However, the code does not compile and I get an exception where I call bm.Compress(). It says:

Error CS1503: Argument 3: cannot convert from 'Java.IO.FileOutputStream' to 'System.IO.Stream'

Solution

  • Okay this is how I got it working:

        Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);
    
        ContextWrapper cw = new ContextWrapper(this.ApplicationContext);
        File directory = cw.GetDir("imgDir", FileCreationMode.Private);
        File myPath = new File(directory, "test.png");
    
        try 
        {
            using (var os = new System.IO.FileStream(myPath.AbsolutePath, System.IO.FileMode.Create))
            {
                bm.Compress(Bitmap.CompressFormat.Png, 100, os);
            }
        }
        catch (Exception ex) 
        {
            System.Console.Write(ex.Message);
        }