簡體   English   中英

如何在運行時從SD卡加載ImageButton圖像?

[英]How do I Load ImageButton image at runtime from SD Card?

我想讓用戶從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>

在Java代碼的onCreate方法中,將按鈕分配給變量,然后設置資源或位圖,將使用BitmapFactory類從文件中解碼該資源或位圖。

Bitmap bmp = BitmapFactory.decodeFile("path/to/file");
ImageButton button1 = (ImageButton)findViewById(R.id.imageButton1);
button1.setImageBitmap(bmp);

這樣嘗試

  ImageButton img = (ImageButton)findViewById(R.id.imageButton2);
  Bitmap bmp = BitmapFactory.decodeFile(path);
  img.setImageBitmap(bmp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM