Android – how do I save an image to internal storage and display it on another activity?

I work on xamarin.android. I have two activities that need to display the same image. On the first screen, I download it from the web URL and display it, but I don't want to perform the same operation on the second screen. I want to download it on the first screen and save it to internal storage, and then just retrieve it from there to display the second activity. How can I do this?

This is the code I used in the 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, in order to save the image, this is what I inspired in 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 is not compiled. I get an exception. I call BM. Compress(). It says:

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

resolvent:

Well, that's how I work:

    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);
    }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>