简体   繁体   中英

How to save a BLOB data type file on a device?

Any help on how to get a file which is saved in MySQL and then save it in a folder on my mobile phone.

I am working on an android application which in some stage it selects files from the database, these files are stored as BLOB....then I would like to save those selected files on my device (Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Testing/")

Obviously I am connecting android to MySQL using PHP. How can I achieve this?

I think, you should convert Blob to Bitmap at first, and then covert that Bitmap to a File and save this file on your external storage. For example, if you're getting image from MySql database:

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM