本文介绍了在xamarin中显示文件路径中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 differenceexample:

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

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

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

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

和( ¬/ HTC Desire 620 /内部存储/存储/模拟/ 0 /测试/ 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/

推荐答案

你能试试吗?

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

这篇关于在xamarin中显示文件路径中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:20