2

ユーザーが SD カードから自分の画像を読み込んで ImageButton として使用できるようにしたいと考えています。これが私のmain.xmlコードです

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/heart"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:src="@drawable/me" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:cropToPadding="true"
        android:scaleType="fitCenter"
        android:src="@drawable/mylove" />

</LinearLayout>
4

2 に答える 2

1

JavaコードのonCreateメソッドで、ボタンを変数に割り当ててから、BitmapFactoryクラスを使用してファイルからデコードされるリソースまたはビットマップを設定します。

Bitmap bmp = BitmapFactory.decodeFile("path/to/file");
ImageButton button1 = (ImageButton)findViewById(R.id.imageButton1);
button1.setImageBitmap(bmp);
于 2012-11-12T05:33:30.023 に答える
0

このようにしてみてください

  ImageButton img = (ImageButton)findViewById(R.id.imageButton2);
  Bitmap bmp = BitmapFactory.decodeFile(path);
  img.setImageBitmap(bmp);
于 2012-11-12T05:29:16.127 に答える