how to save image taken from camera and show it to listview - crashes with IllegalStateException
As per you create database statement
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, " + KEY_TITLE
+ " text not null, ......+KEY_COMMENTS+" text not null,"+KEY_IMAGE+" imageblob BLOB);";
you are mentioned that
"+KEY_IMAGE+" imageblob BLOB
so the column value is "imageblob imageblol BLOB" syntax wise it is not a correct sql statement.
so just remove the "imageblob" from there and try that, it may be solve your problem....
Android How to save camera images in database and display another activity in list view?
Create DataBase helper class like this..
On Capturing the image insert the image by converting into bytes:
Imagehelper help=new Imagehelper(this);
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
help.insert(byteArray);
}
To retrieve Form the Database:
Imagehelper help=new Imagehelper(this);
Cursor c= help.getAll();
int i=0;
if(c.getCount()>0)
{
Bitmap[] array=new Bitmap[c.getCount()];
c.moveToFirst();
while(c.isAfterLast()==false)
{
byte[] bytes=c.getBlob(c.getColumnIndex("imageblob"));
array[i]=BitmapFactory.decodeByteArray(bytes, 0, 0);
c.moveToNext();
i++;
}
Log.e("Bitmap length",""+array.length);
}
Pass this Bitmap array to ListView
Error while trying save and retrieve images into database in android
Instead of just looking at the errors in logcat, look at the warnings. I believe you will see a warning that looks similar to the following:
WARN CursorWindow Window is full: requested allocation x bytes, free space x bytes, window size x bytes
This means that your cursor object is larger than the allocated size, which is normally 2Mb. Your cursor is loading a couple of image / blobs, which might cause it to grow larger than 2Mb.
Solutions
1 - Read the images one by one. Instead of doing a query that selects everything e.g. SELECT * FROM contacts ... change it to read one or two images at a time by indexing the results. You can probably workout how much space you need by looking at the size of your images.
2 - Don't save the images in the database. It might be better to save the images in a directory on the SDcard and then just save the URI's into the db and read them into your cursor. then leave the decoding of the images to your application, which has much more memory to work with than your cursor object.
Implementation of Solution 1
So I finally sat down and wrote the code and tested and it works great. So here is the code for solution 1. It works fine for me, obviously your implementation might be different depending on how you store and retrieve your data etc. So here goes
I have put in comments and modified the code so that it's easier to understand.
//This is the part of my Sync method where I start pulling images from the database.
if(isSyncNeeded)
{
android.util.Log.w(" CURSOR FIXES ", "BEGIN...");
databaseHelperimages = new Handler_Database(Screen_Main.this);
SQLiteDatabase dbimages = databaseHelperimages.getReadableDatabase();
//I store my data in arraylists when I read the from the db.
Session.arraylist_pictures_1 = new ArrayList<String>();
Session.arraylist_pictures_2 = new ArrayList<String>();
//Do all your reading here...
// I have another query before this where I get the id of each row in my database. And here, I say, for each id in my database, get the corresponding image in that row.
for (String id : Session.arraylist_allsubmissions_id)
{
Cursor c1 = dbimages.rawQuery("SELECT picture1 FROM submissions WHERE id" + " = " + id, null);
if(c1.getCount() > 0)
{
if(c1.moveToFirst())
{
do
{
try
{
int index = c1.getColumnIndex("picture1");
String entry = c1.getString(index);
Session.arraylist_pictures_1.add(entry);
}
catch (IllegalStateException e)
{
//Do Nothing
}
catch (NullPointerException e)
{
//Do Nothing
}
}
while(c1.moveToNext());
}
}
c1.close();
Cursor c2 = dbimages.rawQuery("SELECT picture2 FROM submissions WHERE id" + " = " + id, null);
if(c2.getCount() > 0)
{
if(c2.moveToFirst())
{
do
{
try
{
int index = c2.getColumnIndex("picture2");
String entry = c2.getString(index);
Session.arraylist_pictures_2.add(entry);
}
catch (IllegalStateException e)
{
//Do Nothing
}
catch (NullPointerException e)
{
//Do Nothing
}
}
while(c2.moveToNext());
}
}
c2.close();
//Continue doing this for all your images.
}
databaseHelperimages.close();
dbimages.close();
android.util.Log.w(" CURSOR FIXES ", "END...");
}
Adapter.getItem(position) crashes application
This is recursive logic:
@Override
public Object getItem(int arg0) {
return getItem(arg0);
}
There's no way for this method to complete, it simply calls itself again and again until the app throws some type of overflow exception. It should be:
@Override
public MyClass getItem(int arg0) {
return tours.get(arg0);
}
Notice how this method returns data from your List.
getting images from database android
Try this. This might help you.
byte[] pic=(cursor.getBlob(position));
ByteArrayInputStream imageStream = new ByteArrayInputStream(pic);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);
how to save and retrive images from sql lite database in android
You can easily add field of BLOB
type in sql, and put images like set of bytes:
//while writing to db:
ByteArrayOutputStream outStr = new ByteArrayOutputStream();
image.compress(CompressFormat.PNG, 100, outStr);
byte[] blob = outStr.toByteArray();
contentValues.put("image", blob);
//while reading from cursor:
byte[] blob = cursor.getBlob("image");
Bitmap image = BitmapFactory.decodeByteArray(blob, 0, blob.length);
deleting an item from listview (using sqlite) results in only removing it from listview.Can't delete it from database
the problem you have is the call of your method delete, it should be like this :
public long deleteItem(myItems itemToDelete) {
try {
return sqlDatabase.delete(DATABASE_TABLE,COLUMN_ID + " = ?",new String[]{itemToDelete.getID()});
}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
return -1;
}
}
and when you press the YES
Button
of AlertDialog
to confirm delete you should delete the item from database, and then reload items from database and notify the adapter of your list to refresh it with the new data :
myItems selectedItem = adapter.getItem(position);
long rows = sqlHandler.deleteItem(selectedItem);
if(rows>0) {
//get new items from database
adapter.setItems(getItemsFromDatabase());
adapter.notifyDataSetChanged();
}
the item will be deleted from database, and the listView
will be refreshed with the new data
Related Topics
Overriding the Power Button in Android
How to Store Hashmap So That It Can Be Retained It Value After a Device Reboot
Android Studio: Exclude Resource File Under Resources Sourcesets
How to Create Ripple Effect in Simple Layout
Android: HTML Anchor Link Works Only Once in Webview
Fragment's Onsaveinstancestate() Is Never Called
Android-Textview Settext in HTML.Fromhtml to Display Image and Text
Phonegap & Android - How to Use The New Select List Style
Flutter: Upgrade The Version Code for Play Store
Picture Taken with Camera Intent Is Low Quality on Imageview (7.0+)
How to Perform Minification and Obfuscation with the Jack Compiler
Recyclerview with Multiple Countdown Timers Causes Flickering
Android: Starting an Activity for a Different Third Party App
How to Handle Empty Response Body with Retrofit 2
How to Replace a Fragment on Button Click of That Fragment
How to Get Screen Display Metrics in Application Class
How to Check If User Is Logged in with Fb Sdk 4.0 for Android