display an image from File path in xamarin

我只是一个虾纸丫 提交于 2019-12-11 07:59:48

问题


I am trying to display image from the path. I can display the image if i move the image to drawable but i want to display am image from he image Path. I tried using imageBitMap with GetImageBitmapFromUrl("image path") but it only display a blank screen. The seconded way i tried is Android.Net.Uri url = Android.Net.Uri.Parse( "the image path")

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

    }

i tried calling the path in view way but does not seem to make a difference example:

(./HTC Desire 620/Internal storage/storage/emulated/0/test/fox.jpeg)

,(HTC Desire 620/Internal storage/storage/emulated/0/test/fox.jpeg)

and (¬/HTC Desire 620/Internal storage/storage/emulated/0/test/fox.jpeg)

but no joy. I would love any help. Unsure why this giving me so much issue/


回答1:


Could you try this ?

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


来源:https://stackoverflow.com/questions/42627655/display-an-image-from-file-path-in-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!