将图像从数据库保存到SD卡

时间:2013-04-02 22:37:31

标签: android sqlite android-sdcard

我想找到一种方法来保存sdcard中sqlite数据库中的图像,如果我使用简单的查询,我可以找到图像数据但是如何保存它们,例如sdcard/temp 我搜索了很多,但找不到符合我想要的东西

1 个答案:

答案 0 :(得分:0)

我假设你要从数据库中提取blob并从中获取位图或其他任何内容。您可能需要查看http://developer.android.com/reference/java/io/File.html。您可以使用File。

中的方法为给定路径创建文件

如何将位图编码为blob将影响您对这些位图进行重新序列化的方式。

从这篇文章: Android: problem retrieving bitmap from database


编码到db:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, THUMB_QUALITY, outputStream);
mByteArray = outputStream.toByteArray();  // write this to database as blob

从db:

重新序列化
ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(columnIndex));
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream);

保存到文件:

String path = Environment.getExternalStorageDirectory().toString();
File filename = new File(path, "myImg.png");
FileOutputStream out = new FileOutputStream(filename);
bitmapp.compress(Bitmap.CompressFormat.PNG, <<COMPRESSION_AMT>>, out);
out.flush();
out.close();
相关问题