In my app, one issue is there. I am downloading the images from the web and these images are stored in the local database. How do I store these images into the local database?
Asked
Active
Viewed 5,905 times
0
-
Save on SD CARD http://stackoverflow.com/questions/5196330/save-image-to-sdcard-android-directory-problem – Rohit Sharma Sep 13 '11 at 07:26
-
for sdcard, i am getting the out of memory exception – naresh Sep 13 '11 at 07:32
-
Then it's a bad sign for your DB also since you said you'll have a lot of images and the sdcard is more likely to have more capacity than the device itself. – aymeric Sep 13 '11 at 07:34
-
SD Card can hold data in GB .. Whats the size of images you are downloading and storing. – Rohit Sharma Sep 13 '11 at 07:38
-
previously i am storing the images in the sd card when the retrival time i am getting the out of memory exception .please refer this http://stackoverflow.com/questions/7385139/android-out-of-memory-problem. – naresh Sep 13 '11 at 07:56
4 Answers
0
you can do that by several ways, either stored it as BLOB or stored the path of image nice example here

Community
- 1
- 1

Mohammed Azharuddin Shaikh
- 41,633
- 14
- 96
- 115
-
here where u r storing the dowloaded images into DB. Here u r return drawable then how to store this drawable in db? – naresh Sep 13 '11 at 07:27
-
-
-
-
depends, now device are coming with larger memory so there is less chance – Mohammed Azharuddin Shaikh Sep 13 '11 at 07:37
0
To store an image in the database, your table creation should be:
db.execSQL("CREATE TABLE Funny(picid TEXT,myimage BLOB,gender TEXT,country TEXT)");
The image type is blob.
Convert an image URL into bytes form as shown below:
URL url = null;
try {
url = new URL(YOUR IMAGE URL);
}
catch (MalformedURLException e2) {
e2.printStackTrace();
}
URLConnection ucon = null;
try {
ucon = url.openConnection();
}
catch (IOException e1) {
e1.printStackTrace();
}
InputStream is = null;
try {
is = ucon.getInputStream();
}
catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer baf = new ByteArrayBuffer(128);
int current = 0;
try {
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
}
catch (IOException e) {
e.printStackTrace();
}
bb = baf.toByteArray();
help.insert(picid[0],bb, gender1[0], country1[0]);
And insert statement is:
public void insert(String string,byte[] bytes, String gender, String country) {
ContentValues cv = new ContentValues();
cv.put("picid", string);
cv.put("myimage", bytes);
cv.put("gender", gender);
cv.put("country", country);
getWritableDatabase().insert("Funny","gender", cv);
Log.e("inserted", "inserted");
}
I hope this helps you.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Abhi
- 8,935
- 7
- 37
- 60
-
here is any chance of getting out of memory exception. because in my lot of images are there – naresh Sep 13 '11 at 07:35
-
-
-
-
i am asking about creating db ref variable. here u r using db. what is db? – naresh Sep 13 '11 at 07:42
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3396/discussion-between-naresh-and-abhi) – naresh Sep 13 '11 at 07:42
0
I would rather store the image files inside a cache folder and store only the path inside your local DB since it will allow your application to be more scalable and not to overload your DB.
You can use this path to cache your images:
File dataPath = new File(Environment.getDataDirectory(), yourPackageName);
See here documentation.

aymeric
- 3,877
- 2
- 28
- 42
0
Images can be stored as a bytearray to the database. You can get it from a nice example from the following tutorial about how to store and retrieve image from a database.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Chirag_CID
- 2,224
- 1
- 24
- 33