2017-03-06 99 views
1

我試圖從路徑顯示圖像。如果我將圖像移動到可繪製的位置,我可以顯示圖像,但是我想從圖像路徑顯示圖像。我嘗試使用帶有GetImageBitmapFromUrl(「圖像路徑」)的imageBitMap,但它只顯示一個空白屏幕。借調的方式我想是Android.Net.Uri URL = Android.Net.Uri.Parse(下稱「映像路徑」)從xamarin的文件路徑顯示圖像

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慾望620 /內部存儲/存儲/模擬/ 0 /測試/ fox.jpeg)

,(HTC慾望620 /內部存儲/存儲/模擬/ 0 /測試/ fox.jpeg)

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

但沒有喜悅。我會喜歡任何幫助。不確定爲什麼這給了我很多問題/

+0

你從哪裏得到路徑? '/ HTC Desire 620 /內部存儲'不存在於任何手機上... – Cheesebaron

+0

我顯示拍攝照片時獲得的路徑,並在手機上查看手機內存時查看。他們匹配 – oisin1min

+0

不知道你如何檢查手機內存,但在這種情況下正確的路徑是:'/ storage/emulated/0/test/fox.jpeg' – Cheesebaron

回答

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

System.IO.Stream input = Context.ContentResolver.OpenInputStream(uri); 上下文是否大寫。我以以太的方式得到錯誤 – oisin1min

+0

對不起,上下文是一個自定義變量。這是活動,所以你的情況就是this.ContentResolver。我已經更新了我的答案 –

+0

謝謝。什麼[16 * 1024]代表。如果你不介意我要求 – oisin1min

相關問題