Android 从sqlite数据库设置背景图像
在Android中,我希望通过从数据库检索图像来设置背景图像。我已经创建了数据库,并将图像作为BLOB放入其中。数据库在我的资产文件夹中,应用程序可以成功访问和查询数据库(但这可能是因为我还没有访问图像)。但现在我不知道该怎么办。我想我已经完成了如下代码:Android 从sqlite数据库设置背景图像,android,sqlite,Android,Sqlite,在Android中,我希望通过从数据库检索图像来设置背景图像。我已经创建了数据库,并将图像作为BLOB放入其中。数据库在我的资产文件夹中,应用程序可以成功访问和查询数据库(但这可能是因为我还没有访问图像)。但现在我不知道该怎么办。我想我已经完成了如下代码: bgview = (View) findViewById(R.id.bg_display); bgview.setBackgroundResource(bgimage); 但我不知道如何启动它….您可能会创建 InputStream is
bgview = (View) findViewById(R.id.bg_display);
bgview.setBackgroundResource(bgimage);
但我不知道如何启动它….您可能会创建
InputStream is = new ByteArrayInputStream(TheBytesOfTheBlobYouGotFromDB);
然后将其保存到文件中
试一试{
OutputStream out=新文件OutputStream(新文件(文件名))
然后使用解决方案将文件设置为背景:
只需在数据库中只存储图像的路径,而不是图像,您就可以检索路径并将其设置为背景。使用此方法,您可以避免转换为字节
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);