1

データベースからダウンロードされた画像を含む画像ボタンが必要です。つまり、ビルド時には使用できません。私が見たすべての imagebutton の例では、おそらくビルド時に画像を「drawable」フォルダーに入れることを説明しています。実行時に作成/見つかったイメージをロードすることはできますか?

4

2 に答える 2

1

BLOB を使用して DB に画像を挿入します。最初にそのイメージを byte[] に変換しました。

private static final String SQL_GETCONTENTS = 
        "CREATE TABLE " + DB_TABLE + "("+ 
                KEY_CONTENTSID + " TEXT," +
                KEY_IMAGE + " BLOB);";

public void addEntry( String id, byte[] image) throws SQLiteException{
    ContentValues cv = new  ContentValues();
    cv.put(KEY_CONTENTSID, id);
    cv.put(KEY_IMAGE, image);
    database.insert( DB_TABLE, null, cv );
}


public Cursor getEntry(String id ){
    String sql = "select * from " + DB_TABLE + " where " + KEY_CONTENTSID + "='" + id + "'";
    Cursor c = database.rawQuery(sql, null);
    c.moveToFirst();
    return c;
}

画像を取得して設定:

Cursor c = entry.getEntry(idKey);
byte[] image =  c.getBlob(0);
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither = false;
bfOptions.inPurgeable = true;
bfOptions.inInputShareable = true;

thumbImage.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length, bfOptions));

を使用BitmapFactory.Optionsすると、メモリ不足の例外を回避するのに役立ちます。

于 2013-04-26T01:25:07.383 に答える
1

はい、できます。バイト配列からビットマップを作成する必要があります (ファイルまたはデータベースから読み取ることができます)。次に、バイト配列を に変換しますBitmap。このようなもの:

Bitmap bitmap = bytesToBitmap(<your byte array here>);
ImageButton button = (ImageButton)findViewById(R.id.my_button);
button.setImageBitmap(bitmap);

public static Bitmap bytesToBitmap(byte[] bytes)
{
    ByteArrayInputStream imageStream = null;

    try
    {
        imageStream = new ByteArrayInputStream(bytes);
        return BitmapFactory.decodeStream(imageStream);
    }
    catch (Exception ex)
    {
        Log.d("My Activity", "Unable to generate a bitmap: " + ex.getMessage());
        return null;
    }
    finally
    {
        if (imageStream != null)
        {
            try
            {
                imageStream.close();
            }
            catch (Exception ex) {}
        }
    }
}
于 2013-04-26T01:04:31.410 に答える