Ashok Kateshiya
Ashok Kateshiya

Reputation: 699

How to store image in database?

I am working on an application which needs to store image in database. I have tried it by converting image into bytes but it is very slow process.is there any other way to store image in database

Upvotes: 1

Views: 315

Answers (3)

Sagar Maiyad
Sagar Maiyad

Reputation: 12753

Get image form sd card

if (requestCode == SD_REQUEST) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

testimage.setImageBitmap(yourSelectedImage);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}

Save Image

DatabaseAdapter dbHelper = new DatabaseAdapter(Profiles.this);

   dbHelper.open();
   dbHelper.createUserProfiles( byteArray);
   dbHelper.close();

NOw in DatabaseAdapter.java

Define

public static final String U_PIC = "picture";

Then

private long createUserTableContentValues(long id,byte[] byteImage) {
        ContentValues values = new ContentValues();
        values.put(ID, id);
        values.put(U_PIC, byteImage);
return database.insert(IMAGE_INSERT, null, values);
}

Upvotes: 1

yuva ツ
yuva ツ

Reputation: 3703

you can store image path in database in string format..

Upvotes: 0

ashokk
ashokk

Reputation: 447

You can store image path into database and store images in sdcard and retrieve images from imagepath from database

Upvotes: 4

Related Questions