无法将Java.IO.FileOutputStream转换为System.IO.Stream?

时间:2017-06-16 06:16:12

标签: c# xamarin xamarin.android

我试图保存图片并在行中 bitmapImg.Compress(Bitmap.CompressFormat.Png,90,fos); 显示错误无法转换Java.IO .FileOutputStream到System.IO.Stream 。怎么解决?

File file = new File(Android.OS.Environment.DirectoryPictures + File.Separator + "newProdict.png");
            FileOutputStream fos = null;
            try
            {
                fos = new FileOutputStream(file);
                if (fos != null)
                {
                    bitmapImg.Compress(Bitmap.CompressFormat.Png, 90, fos);
                    fos.Close();
                }
            }
            catch (Exception ex) { }

1 个答案:

答案 0 :(得分:1)

当您从java命名空间传递类时,Compress函数需要从.net System.IO.Stream派生的内容,而是使用FileStream:

    try
    {

     string path = Path.Combine(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures).AbsolutePath, "newProdict.png");
     var fs = new FileStream(path, FileMode.Open);
        if (fs != null)
        {
            bitmapImg.Compress(Bitmap.CompressFormat.Png, 90, fs);
            fos.Close();
        }
    }
    catch (Exception ex) { }