1

Androidでは、データベースから画像を取得して背景画像を設定したいのですが。DBを作成し、画像をBLOBとして配置しました。DBはアセットフォルダーにあり、アプリはDBに正常にアクセスしてクエリを実行できます(ただし、まだ画像にアクセスしていないことが原因である可能性があります)。でも今はここからどこへ行けばいいのかわからない。次のようなコードでコードを完成させたと思います。

bgview = (View) findViewById(R.id.bg_display);
bgview.setBackgroundResource(bgimage);

でもどうやって始めたらいいのかわからない…。

4

2 に答える 2

1

あなたが作成するかもしれません

InputStream is = new ByteArrayInputStream(TheBytesOfTheBlobYouGotFromDB);

ファイルに保存します

{OutputStream out = new FileOutputStream(new File(FileName));を試してください。

int read = 0;
byte[] bytes = new byte[1024];

while ((read = is.read(bytes)) != -1) { out.write(bytes, 0, read); }

is.close();
out.flush();
out.close();
} catch (IOException e) {}

次に、ソリューションを使用してファイルを背景として設定します。 ビューの背景としてビットマップを配置します。

Matrix Mat = new Matrix();

/// FileName is the file where you saved the 'is'
Bitmap Source = BitmapFactory.decodeFile(FileName);
Bitmap Destination = Bitmap.createScaledBitmap( Source, DisplayWidth, DisplayHeight, true );

Source = Bitmap.createBitmap( Destination, 0, 0, Destination.getWidth(), Destination.getHeight(), Mat, true );

/// Use the 'Source' here
bgview.setBackgroundResource(Source);
于 2012-04-27T22:35:43.723 に答える
1

画像ではなく画像のパスのみをデータベースに保存するだけで、パスを取得して背景として設定できます。これを使用すると、バイトへの変換を取り除くことができます。

于 2012-04-28T07:35:19.407 に答える