问题
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