2017-03-06 17 views
1

パスから画像を表示しようとしています。私はdrawableに画像を移動する場合、私は画像を表示することができますが、私は彼の画像パスから画像を表示したい。 GetImageBitmapFromUrl( "image path")で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)

と(¬/ HTC Desire 620 /内部ストレージ/ストレージ/エミュレート/ 0 /テスト/ fox.jpeg)

喜びはありません。私はどんな助けも大好きです。なぜこれが私にそんなに大きな問題を与えるのかわからない/

+0

ここからパスを取得しますか? '/ HTC Desire 620/Internal Storage'は携帯電話には存在しません... – 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

関連する問題