0

I have an camera Activity like this:

public void startCamera() 
{
    Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
    String fileName = "testphoto.jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,
            "Image capture by camera");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, IMAGE_CAPTURE);
}

What should I do to save this image to a SQLite database?


I have code like this:

fileName = helper.getKdStore(c) + "_" + System.currentTimeMillis() + ".jpg";
_path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/" + fileName;

In start camera:

Cursor c = helper.getById(almagId);
c.moveToFirst();
File file = new File(_path, null);
try {
    file.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, IMAGE_CAPTURE);
System.out.println(file);

Dbhelper helper = new Dbhelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("update alfamapp set gambar ='"+file+"' where kdstore='"+helper.getKdStore(c)+"'");
db.close();
helper.close();

System.out.println(db);

and I create method public void load() to display my image:

detGam.setImageDrawable(Drawable.createFromPath(_path));

but my application can't display the image. Thank you for your help.

Sam
  • 86,580
  • 20
  • 181
  • 179
akubabas
  • 473
  • 5
  • 13
  • 28

1 Answers1

2

Saving Image in to sqlite is bad idea, however if you still want to go further, there here is code

public static void insertPicture(byte[] data)
{

    SQLiteDatabase db = thisAct.openOrCreateDatabase(ConstantCodes.IMAGE_DATABASE, 2, null);
    ContentValues values = new ContentValues(1);
    values.put("photo", data);
    db.insert("photos", null, values);
    db.close();

} 
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • i have database image like this `public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE image (_id integer primary key AUTOINCREMENT,kdstore text,image1 BLOB,image2 BLOB,image3 BLOB)");}`what should i do??thx – akubabas Feb 07 '12 at 03:26
  • you mean you are storing 3 image in one row ? – Lucifer Feb 07 '12 at 03:44
  • yes exactly!!i want to save 3 image in one row..what should i do?? – akubabas Feb 07 '12 at 04:23
  • i have error in `SQLiteDatabase db = **thisAct**.openOrCreateDatabase(**ConstantCodes**.IMAGE_DATABASE, 2, null);` i should create class **ConstantCodes** ?? i'm sory, i'm begginer in android developer :) – akubabas Feb 07 '12 at 04:54
  • that is name if my database , you can use your database name like "photo.db" instead of ConstantCodes.IMAGE_DATABASE – Lucifer Feb 07 '12 at 04:56
  • thisAct is object of current Activity, you can use your own , or put context variable there – Lucifer Feb 07 '12 at 05:17
  • and what mean **ConstantCodes** ??sory i'm very confused :) nad btw thx :) – akubabas Feb 07 '12 at 06:32
  • it is a class that used to define constant values, – Lucifer Feb 07 '12 at 06:34
  • if not in the form of BLOB,in what way i can save image in to sqlite??please give your suggestion :) – akubabas Feb 09 '12 at 01:37
  • yes, then you should store it on SD card and you should store its path in database , it is best option while working with images. because images are bigger in size then text data, so it is hard to work with images – Lucifer Feb 09 '12 at 01:54
  • can you give a simple code to save image path into database?and retrieve it back??thx :) – akubabas Feb 09 '12 at 02:33
  • first try your self, and then ask it for help, when you stuck in to a problem. – Lucifer Feb 09 '12 at 03:15
  • i can save image path to database,,now i stuck with retrieve it..please give me a clue to do this..thx :) – akubabas Feb 09 '12 at 03:27
  • your answer is so helpful thank you,,i have script to retrive imagepath from database like this `detGam.setImageDrawable(Drawable.createFromPath(helper.getGamb(c)));`but my application force close,can you help me once again??thank you :) – akubabas Feb 09 '12 at 03:55
  • ok thank you , this way it helps others too. well after saving path of image, now you need to retrieve the path and try to create image from that path, so your problem will be solved. – Lucifer Feb 09 '12 at 04:27
  • how can i do this??i'm newbie with android development,,this is my script to retrieve image back `detGam.setImageDrawable(Drawable.createFromPath(helper.getGamb(c)));`..but my application force close..what should i do?? – akubabas Feb 09 '12 at 04:30
  • ok, look at this ,link http://stackoverflow.com/questions/6725718/android-display-image-from-sd-card in question part it is the code you wanted. – Lucifer Feb 09 '12 at 04:35
  • i dont understand with this clue,,i try to put this code but i allways have an error..:( i have this code `fileName =helper.getKdStore(c)+"_"+System.currentTimeMillis()+ ".jpg"; _path=Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";`..and my code to retrieve back like this `detGam.setImageDrawable(Drawable.createFromPath(_path));`..but image can't display in my application..i'm so frustased :( – akubabas Feb 09 '12 at 04:51
  • i have upload my code..please check how i can retrieve image..thank you – akubabas Feb 09 '12 at 06:28