1

I am making a quiz app which requires me to be able to get an image from a database BLOB or image path stored in the database. However i have looked around and a lot of people suggest using a file path, the problem is i don't know where to store the image if i use the file path method.

Do i store it somewhere in the app such as the resources folder?, a lot of examples use SD cards but is it possible to save an image to SD card from a database and if so surely that would mean i have two images one in database and one on SD card.

Where is the best place to store a images for a quiz app that i can use on any phone an will have access to said images? and how ?.

Thanks in advance.

Wuffle
  • 73
  • 1
  • 14

1 Answers1

1

Image storage must be performed in some directory and the corresponding paths of the image must be stored in the database.

enter image description here

There will be times when you will accessing your images from one acivity then the other, in that case you will just need to pass the path of the image from activity one to activity two and then retrieve the image from the directory to display in activity two. Image storing and loading from databases may turn out to be a pain when the size of the images will start increasing. For learning how to store images, Give an eye to this

CODE EXAMPLE

private void SaveImage(Bitmap finalBitmap) {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");    
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

}

In the above code, the line

 Environment.getExternalStorageDirectory()

is refering to android/data folder. you can create folder inside upto any level, like android/data/folderone/folderTwo/folderThree .

Note: However you need to first fetch the images from server for the first time and store them in device.If you are thinking of bundling up the images along with the app, put all of your images in res/drawable folders.(if no web server functionality is involved)


Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
  • I assume you mean directory in the phone and not the application itself, in which case how do i store the image to a directory in the first place without dragging the image their. – Wuffle Apr 02 '16 at 23:51
  • you must be aware that in file explorer at **android/data/** there are certain folders.Here you can create your own directory and can store your images specific to your app. check out the link in the answer – nobalG Apr 02 '16 at 23:55
  • To save the image to a directory i first need to have the image saved somewhere that can be accessed by any phone, such as a database? in which case why save it twice if its saved to a database? – Wuffle Apr 03 '16 at 00:05
  • In database you will be storing only path, not image, suppose your image is in folder **android/data/yourappname/images** , then in third column of database as given in above image , you will store ***android/data/yourappname/images*** , not the image itself. – nobalG Apr 03 '16 at 00:07
  • I understand that but how do i save the image to android/data/ in the first place through code. – Wuffle Apr 03 '16 at 00:14
  • I added some code and some more explanation in answer. – nobalG Apr 03 '16 at 00:18
  • I understand how to create a directory to store images but i need to store the image within my app somewhere in order for me to save it to a directory i created. In which i case where do i store the image in my app before i save it to a directory. If i don't have the image within my app then how can i store it to a directory. Sorry for the confusion. – Wuffle Apr 03 '16 at 00:27
  • See if there is no web service functionality, nothing to worry about all this database and file explorer stuff, just put your images in res/drawable, but to make your app dynamic, you must implement the above scenario. – nobalG Apr 03 '16 at 00:34