How to store images in SQLite database (insert, update, delete and fetch) in your Android App?



How to store images in SQLite database (insert, update, delete and fetch) in your Android App?

How to store images in SQLite database (insert, update, delete and fetch) in your Android App?

This video shows simple steps to store the image files in your local SQLite database. For storing the Images in the database, unlike simple text or numbers, it uses BLOB field. It first converts the image file (or image) into bitmap. Then it converts the bitmap into bytes by compressing the bitmap using the PNG compress format.

It uses the same method to either insert or update the image field in the SQLite database.

It does the reverse while fetching the information from the SQLite database. It first retrieves the byte array information. Then it converts the byte array into Bitmap using the decodeByteArray method from BitmapFactory.

It uses an ImageView widget to display or show the output fetched from the database column.

In the deletion method, it simply deletes the row from the database using the where clause for the respective row (entry).

In this tutorial it has hardcoded the Name (Key/ index) field of the database as “MyImage”. But in all practical scenario this field entry should be taken from the user (will change as per the use-case).

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details can be found in the below link:
https://programmerworld.co/android/how-to-store-images-in-sqlite-database-insert-update-delete-and-fetch-in-your-android-app/

Java code:

package com.programmerworld.imgefileinsqlitedatabase;

public class MainActivity extends AppCompatActivity {

private EditText editTextFileName;
private TextView textViewStatus;
private ImageView imageView;

private mySqliteDBHandler sqliteDBHandler;
private SQLiteDatabase sqLiteDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

editTextFileName = findViewById(R.id.editTextTextFileName);
textViewStatus = findViewById(R.id.textViewStatus);
imageView = findViewById(R.id.imageView);

try {
sqliteDBHandler = new mySqliteDBHandler(this, “ImageDatabase”, null, 1);
sqLiteDatabase = sqliteDBHandler.getWritableDatabase();
sqLiteDatabase.execSQL(“CREATE TABLE ImageTable(Name TEXT, Image BLOB)”);
}
catch (Exception e){
e.printStackTrace();
}
}

public void buttonInsert(View view){
String stringFilePath = Environment.getExternalStorageDirectory().getPath()+”/Download/”+editTextFileName.getText().toString()+”.jpeg”;
Bitmap bitmap = BitmapFactory.decodeFile(stringFilePath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
byte[] bytesImage = byteArrayOutputStream.toByteArray();

ContentValues contentValues = new ContentValues();
contentValues.put(“Name”, “MyImage”);
contentValues.put(“Image”, bytesImage);
sqLiteDatabase.insert(“ImageTable”, null, contentValues);
textViewStatus.setText(“Insert Successful”);
}

public void buttonUpdate(View view){
String stringFilePath = Environment.getExternalStorageDirectory().getPath()+”/Download/”+editTextFileName.getText().toString()+”.jpeg”;
Bitmap bitmap = BitmapFactory.decodeFile(stringFilePath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
byte[] bytesImage = byteArrayOutputStream.toByteArray();

ContentValues contentValues = new ContentValues();
contentValues.put(“Name”, “MyImage”);
contentValues.put(“Image”, bytesImage);

sqLiteDatabase.update(“ImageTable”, contentValues, null, null);
textViewStatus.setText(“Update Successful”);
}

public void buttonDelete(View view){
if (sqLiteDatabase.delete(“ImageTable”, “Name=”MyImage””, null) GREATER_THAN 0){
textViewStatus.setText(“Deletion successful”);
}
}

public void buttonFetch(View view){
String stringQuery = “Select Image from ImageTable where Name=”MyImage””;
Cursor cursor = sqLiteDatabase.rawQuery(stringQuery, null);
try {
cursor.moveToFirst();
byte[] bytesImage = cursor.getBlob(0);
cursor.close();
Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytesImage, 0, bytesImage.length);
imageView.setImageBitmap(bitmapImage);
textViewStatus.setText(“Fetch Successful”);
}
catch (Exception e){
textViewStatus.setText(“ERROR”);
}
}
}

Comments are closed.