在xamarin中显示来自文件路径的图像

时间:2017-03-06 14:06:30

标签: c# image url xamarin filepath

我正试图从路径中显示图像。如果我将图像移动到可绘制但我想显示图像路径中的图像,我可以显示图像。我尝试将imageBitMap与GetImageBitmapFromUrl(“图像路径”)一起使用,但它只显示一个空白屏幕。我尝试的借调方式是Android.Net.Uri url = Android.Net.Uri.Parse(“图片路径”)

namespace SetPictureUrl
[Activity(Label = "SetPictureUrl", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);`{


        ImageView  imagen = FindViewById<ImageView>(Resource.Id.demoImageView);
        //------------------ i tried but did not work. the screen is blank
        //  var imageBitmap = GetImageBitmapFromUrl("my image path");
        //  imagen.SetImageBitmap(imageBitmap);


        //---------------- i tried but did not work. the screen is blank
        Android.Net.Uri url = Android.Net.Uri.Parse("./HTC Desire 620/Internal storage/storage/emulated/0/test/fox.jpeg");
        imagen.SetImageURI(url);


        //----- this work but is not the way i wish to do it. my main program work with file paths 
        // imagen.SetImageResource(Resource.Drawable.fox);

    }

我尝试以视图方式调用路径,但似乎没有任何区别 例如:

(./ HTC Desire 620 /内部存储/存储/模拟/ 0 / test / fox.jpeg)

,(HTC Desire 620 /内部存储/存储/模拟/ 0 / test / fox.jpeg)

和(¬/ HTC Desire 620 /内部存储/存储/模拟/ 0 / test / fox.jpeg)

但没有快乐。我很乐意帮忙。不确定为什么这给了我这么多问题/

1 个答案:

答案 0 :(得分:1)

你可以尝试一下吗?

                Android.Net.Uri uri = Android.Net.Uri.FromFile(new Java.IO.File(filePath));

                System.IO.Stream input = this.ContentResolver.OpenInputStream(uri);

                //Use bitarray to use less memory                    
                byte[] buffer = new byte[16 * 1024];
                using (MemoryStream ms = new MemoryStream())
                {
                    int read;
                    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    pictByteArray = ms.ToArray();
                }

                input.Close();

                //Get file information
                BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
                Bitmap bitmap = BitmapFactory.DecodeByteArray(pictByteArray, 0, pictByteArray.Length, options);

                imagen.SetImageBitmap(bitmap);