如何在设备上保存BLOB数据类型文件?

时间:2014-02-06 09:04:29

标签: php android mysql

有关如何获取保存在MySQL中的文件然后将其保存在手机上的文件夹中的任何帮助。

我正在开发一个Android应用程序,在某个阶段它从数据库中选择文件,这些文件存储为BLOB ....然后我想将这些选定的文件保存在我的设备上(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Testing/")

显然我正在使用PHP将android连接到MySQL。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

我认为,您应该首先将Blob转换为Bitmap,然后将该Bitmap转换为文件并将此文件保存在外部存储上。 例如,如果您从MySql数据库获取图像:

Blob blob = // get Blob from MySql database 
byte [] bytes = blob.getBytes(1, (int) blob.length());
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Testing/";
File dir = new File(filePath);
File file = new File(dir, "filename.png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();