Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store images to SDCard and get string file path

 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic