Programming newbie here.
I am making an app that stores the data of all the items in a store.
The app has an activity that shows the item names and quantity in stock in a listview. (This is done)
Clicking on a listview item takes you to another activity which shows the details of that particular item, which includes an image (i don't know how to do this).
I added a column into the database with BLOB type like this.
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_ITEMS_TABLE = "CREATE TABLE "
+ ItemEntry.TABLE_NAME + " ("
+ ItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ItemEntry.COLUMN_ITEM_IMAGE + "BLOB, "
+ ItemEntry.COLUMN_ITEM_NAME + " TEXT NOT NULL, "
+ ItemEntry.COLUMN_ITEM_QUANTITY
+ " INTEGER NOT NULL DEFAULT 0, "
+ ItemEntry.COLUMN_ITEM_COST_PRICE
+ " INTEGER NOT NULL, "
+ ItemEntry.COLUMN_ITEM_SELLING_PRICE + " INTEGER NOT NULL);";
db.execSQL(SQL_CREATE_ITEMS_TABLE);
}
I have put an "Add image" button in the Details activity, when clicked, it should take the user to the gallery and allow to select one photo and add the photo to the database and show the photo in an imageview in the same activity.
Here is my code for onClickListener on the addImage button.
addImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
in.setType("image/*");
in.putExtra("crop", "true");
in.putExtra("outputX", 100);
in.putExtra("outputY", 100);
in.putExtra("scale", true);
in.putExtra("return-data", true);
startActivityForResult(in, 1);
}
});
Here is the code onActivityResult method.
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Uri imageUri = data.getData();
mImage.setImageURI(imageUri);
}
}
In the above code, i don't know how to store the photo in the database for every item.
I checked other answers which uses get("data") method which is leading to a Nullpoint exception. Some answers use TEXT type to create the column for image and others use BLOB, so it is really confusing.
I am not concerned about slowing down the app or running out of memory, so please help me do this in the simplest way possible.