jQuery in Action, 2nd edition*
The moose likes Android and the fly likes How to store images to SDCard and get string file path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to store images to SDCard and get string file path " Watch "How to store images to SDCard and get string file path " New topic
Author

How to store images to SDCard and get string file path

Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 68

Hi,
I am looking to save a large amount of images
to a SQLite DB (150+).I was thinking of saving as
base64 string but am concerned about space.
So was wondering how to store images on SDCard as files and put a string filepath to DB to allow retrieval of images when a row is called.

Thanks
Ciaran

Ps this should be in android forum , does anyone no how I can redirect and post there


I love deadlines, I love the whooshing sound they make as they fly by...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
I agree that storing as files is better than in the DB. SD storage is explained here: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal


Ping & DNS - my free Android networking tools app
Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 68

Yep code from site great guide, works fine:

// check if ext storage is available to write
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {

canSaveToExternalSD = true;
String root = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString()
+ "/saved_images";
File myDir = new File(root);
myDir.mkdirs();
String photoName = "image" + diveNum + ".jpg";
File file = new File(root + "/" + photoName);
if (file.exists()) {
file.delete();
makeToast("Photo currently exists!'");

try {
FileOutputStream out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);// compress
// image fro
// output
out.flush();
out.close();

I forgot to add the WRITE_EXTERNAL_STORAGE permission in manifest which did give Ne FileNotFound exceptions as directory was never created, just something to watch out for
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store images to SDCard and get string file path
 
Similar Threads
how to create sdcard and mount on the emulator
How to display Images stored on sdcard?
Capture image using android emulator
Store an image in the sdcard
Image gets stored