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>
4

4 回答 4

1

像你一样设置xml之后,

只需添加以下代码即可将图像设置为您的图像视图

ImageView img = (ImageView)findViewById(R.id.Imageview1);
img.setImageResource(R.drawable.yourimage);
于 2013-07-22T07:46:15.037 回答
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);

于 2013-07-22T07:48:08.613 回答
1

如果上述分辨率不正确,那么试试这个,它会从你想要的页面加载图像:

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;
        }
于 2013-07-22T07:51:51.813 回答
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" />
于 2013-07-22T07:39:46.967 回答