Save image in Database

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Save image in Database

Postby Nemat » Tue Sep 30, 2008 1:26 pm

Hi Frnds.....

I have to save an image in database.How can it be possible in SDK-1.0??

Thanks in Advance
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Top

Postby ninor » Tue Sep 30, 2008 4:15 pm

User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby Nemat » Thu Oct 02, 2008 4:24 pm

I have studied the whole procedure but I dont find any way to save image in database.
I think I should first save it in a file and then save its path in database........
But how can I write image in a file???
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Postby ninor » Thu Oct 02, 2008 8:16 pm

Sorry, I didn't understand you...

here's a complete example.
Maybe you'll have to search more .java files in that example.

http://code.google.com/p/apps-for-android/source/browse/trunk/Photostream/src/com/google/android/photostream/ViewPhotoActivity.java
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby plusminus » Fri Oct 03, 2008 12:38 am

Nemat wrote:I have studied the whole procedure but I dont find any way to save image in database.
I think I should first save it in a file and then save its path in database........
But how can I write image in a file???


Saving only the path in the Database is one possibility (I do that for my OpenStreetMapView).
You can add an Image to the DataBase by using

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final ContentValues cv = new ContentValues();
  2.  
  3. ...
  4.  
  5. cv.put(T_MYTABLE_IMAGEDATA, A_BYTEARRAY);
  6.  
  7. this.mDatabase.insert(T_MYTABLE, null, cv);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


You could get the image as a byte-Array, by storing (compressing) it to a ByteArrayOutputStream.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Nemat » Fri Oct 03, 2008 2:44 pm

Thanks PlusMinus,
Now I am able to save my image in database.

Thanks a lot
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Top

Postby JoyLakh » Tue Apr 07, 2009 3:09 pm

Hi Nemat,

I will be very thankful if you could post how you retrieved that bytearray from the database, i tried but could not come up with a solution.

Eeeeeeeeeeeeeagerly waiting.........
Celebrate Life.
JoyLakh
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Dec 16, 2008 5:42 am
Location: Bangalore, India

Postby JoyLakh » Wed Apr 08, 2009 2:12 pm

Hello guys,

I managed to come up with a solution and I want to share it with you as it took me relatively long time and I dont want anyone to loose time on this. In the below pasted code, I created a database(myCoolDB_21) and inserted an image(source.jpg) in its table(images). I used the sql datatype blob( binary large object ), so that I can store the image in the form of bytes. To confirm that I have successfully inserted the image in the database, I retrieved it and made a copy of it(first copy.jpg).

Only pre-requisite is virtual sd-card with source.jpg in it, and here goes the code:

public class DataBaseWork extends ListActivity
{

private final String MY_DATABASE_NAME = "myCoolDB_21";
private final String MY_DATABASE_TABLE = "images";
private static final String DATABASE_CREATE =
"create table images (_id integer , image blob );";
public static final String KEY_IMAGE = "image";
public static final String KEY_ROWID = "_id";

// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SQLiteDatabase myDB = null;
try
{

myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);

FileInputStream fis = new FileInputStream("/sdcard/source.jpg");
byte[] hash = new byte[fis.available()];
fis.read(hash);
fis.close();

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_IMAGE, hash);

myDB.execSQL(DATABASE_CREATE);

myDB.insert(MY_DATABASE_TABLE, null, initialValues);

Cursor c = myDB.query(MY_DATABASE_TABLE, new String[] {KEY_ROWID, KEY_IMAGE}, null, null, null, null, null);

// Get the indices of the Columns we will need
int imageColumn = c.getColumnIndex("image");

// Check if our result was valid.
if (c != null)
{
// Check if at least one Result was returned.
if (c.getCount()!=0)
{
c.moveToFirst();
// Loop through all Results
do
{
FileOutputStream fos = new FileOutputStream("/sdcard/firstCopy.jpg");
fos.write(c.getBlob(imageColumn));
fos.flush();
fos.close();
} while (c.moveToNext());
}
}

} catch(Exception ex){

}finally{
if (myDB != null)
{
myDB.close();
SQLiteDatabase.releaseMemory();
}
}
}
}
Celebrate Life.
JoyLakh
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Dec 16, 2008 5:42 am
Location: Bangalore, India

Postby prerna » Tue Apr 13, 2010 8:29 pm

Hi Thanks everyone for your great responses to this thread which helped me understand the concepts. But there is one thing that n my case I want to choose from the sdcard/phone memory the images and then add that image. How do I do that? Thanks again in advance.
prerna
prerna
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Mar 04, 2010 12:54 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests