我知道我可以使用下面的代码在系统布局中显示图像, 但我希望在我的自定义布局中显示图像,我该怎么做?谢谢!
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 :(得分:1)
完成xml后,
只需添加以下代码即可将图像设置为imageview
ImageView img = (ImageView)findViewById(R.id.Imageview1);
img.setImageResource(R.drawable.yourimage);
答案 1 :(得分: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);
答案 2 :(得分: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;
}
答案 3 :(得分: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" />