This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 72

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: 41598
    
  55
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: 72

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
 
jQuery in Action, 2nd edition
 
subject: How to store images to SDCard and get string file path