[英]How do I Load ImageButton image at runtime from SD Card?
I want to let the user to load their own images from an SD Card to be used as an ImageButton. 我想让用户从SD卡加载自己的图像以用作ImageButton。 Here is my main.xml code
这是我的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>
In the onCreate method of your java-code assign the button to a variable, and then set a resource or Bitmap which will be decoded from a file using the BitmapFactory class. 在Java代码的onCreate方法中,将按钮分配给变量,然后设置资源或位图,将使用BitmapFactory类从文件中解码该资源或位图。
Bitmap bmp = BitmapFactory.decodeFile("path/to/file");
ImageButton button1 = (ImageButton)findViewById(R.id.imageButton1);
button1.setImageBitmap(bmp);
Try like this 这样尝试
ImageButton img = (ImageButton)findViewById(R.id.imageButton2);
Bitmap bmp = BitmapFactory.decodeFile(path);
img.setImageBitmap(bmp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.