2013-07-22 107 views
1

我知道我可以使用下面的代碼在系統佈局中顯示圖像,但我希望以自定義佈局顯示圖像,我該怎麼做?謝謝!我可以在自定義佈局中顯示圖像嗎?

public void onClick(View v) { 
        // TODO Auto-generated method stub 
        int id = v.getId(); 
        Intent intent = new Intent(); 
        intent.setAction(Intent.ACTION_VIEW); 
        intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); 
        startActivityForResult(intent,ForBrowse); 
} 



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/border_ui" 
    android:orientation="vertical" 
    android:paddingTop="3dip" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/btnClose" 
     style="@style/myTextAppearance" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/myreturn" /> 

</LinearLayout> 
+0

是你問如何從xml佈局文件顯示圖像? – ObieMD5

+0

嘗試添加imageView到您的佈局! –

+0

@Tarsem他已經在他的佈局中有ImageView。 – ObieMD5

回答

1

如果上面的分辨率也不行,那就試試這一個,它從Tha頁面加載圖像,你想:

String url = "file://" + arrPath[id]), "image/*"; 
Bitmap bmp = fromURL(url); 
imgview.setImageBitmap(bmp); 
and write this function: 

public static Bitmap fromURL(String src) { 
     try { 
      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url 
        .openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap mybitmap = BitmapFactory.decodeStream(input); 

      return bitmap; 

     } catch (Exception exception1) { 
      return null; 
     } 
0

如果您想您從XML文件佈局,以添加一個圖像,然後

<ImageView 
    android:id="@+id/imageView1" 
    android:src="@drawable/book" <- Change this to your drawable item 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 
1

設置XML了,你做了之後,

只需將以下代碼添加到設置圖像到您的ImageView

ImageView img = (ImageView)findViewById(R.id.Imageview1); 
img.setImageResource(R.drawable.yourimage); 
1
Intent intent = new Intent(this, YourImageViewerActivity.class); 
Bundle bundle = new Bundle(); 
bundle.putString("data", yourImageUri); 
intent.putExtras(bundle); 
startActivity(intent); 

onCreate()方法:

Bundle bundle = getIntent().getExtras(); 
String yourImageUri= bundle.getString("data"); 
Bitmap bitmap = BitmapFactory.decodeFile(yourImageUri); 
ImageView myImageView = (ImageView)findViewById(R.id.imageView1); 
myImageView.setImageBitmap(bitmap); 

相關問題