2013-07-22 86 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); 

相关问题