0

I am confused about how to do this..

For database I use:

public void insertImage(byte[] bytes) {
         ContentValues cv = new ContentValues();

         cv.put("imageblob", bytes);
         Log.e("inserted", "inserted");
         dbHelper.getWritableDatabase().insert("Image", "imageblob", cv);

     }

     public byte[] getImage(Cursor c) {
         return (c.getBlob(1));
     }

The database helper:

 ...
 public static final String KEY_COMMENTS = "comments";
public static final String KEY_IMAGE = "imageblob";

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);";

The myItems:

public class myItems {
     ....
     String comments;
 byte[] imageblob;
      public byte[] getImage() {
      return imageblob;
     }

 public void setImage(byte[] imageblob) {
      this.imageblob = imageblob;
    }

Now, I have the listview in MainActivity.From another activity,ShowList, I have a button with which to take the photo:

   case R.id.photobtn:   
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
    break;

In onActivityResult :

   protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

                  if(resultCode == 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();

                        sqlHandler.insertImage(byteArray);
                  }

My adapter:

public class myAdapter extends BaseAdapter{

...

    public myAdapter(Context context, ArrayList<myItems> list) {
        this.context = context;
        this.setItems(list);
    }

        @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        myItems theItems = getItems().get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.showadapterlist, null);

        }

           ...
           TextView Comments = (TextView) convertView.findViewById(R.id.text_comments);
        Comments.setText(theItems.getComments());

           ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
        myImage.setImageBitmap(getImageFromBLOB(theItems.getImage()));

           return convertView;
          }

    public static Bitmap getImageFromBLOB(byte[] mBlob)
      {
        byte[] bb = mBlob;
        return BitmapFactory.decodeByteArray(bb, 0, bb.length);
     }

    public ArrayList<myItems> getItems() {
        return items;
    }

    public void setItems(ArrayList<myItems> items) {
        this.items = items;
    }

My problems:

1) The listview lies in MainActivity.The photo is taken from ShowList activity.In ShowList activity I have:

case R.id.OKbtn:

String mycomments = comments.getText().toString();              
byte[] myimageblob=null;
 String query = "INSERT INTO MEMOR(title,...comments,imageblob)" +
" values (..+ mytitle + "....+mycomments +  "','" +myimageblob+  "')"   sqlHandler.executeQuery(query);

  Intent k=new Intent(this,MainActivity.class);
  setResult(RESULT_OK,k);
   finish();

How should I handle the "imageblob" value?

2) In MainActivity (where the list view is) I have:

         ...
    myItems theItems = new myItems();   
                                 theItems.setID(c1.getString(c1.getColumnIndex("_id")));
                             theItems.setTitle(c1.getString(c1.getColumnIndex("title")));

      ...
                                 theItems.setComments(c1.getString(c1.getColumnIndex("comments")));
  byte[] blob=c1.getBlob(c1.getColumnIndex("imageblob"));
   ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
   Bitmap bitmap = BitmapFactory.decodeStream(inputStream);         

   myList.add(theItems);

How should I handle the imageblob?

Right now , when I start my application is crashes with "IllegalStateException" at line

" byte[] blob=c1.getBlob(c1.getColumnIndex("imageblob"));"

. If I try c1.setImage it doesn't work.

------------------NEW-------------------------------------------------- In one activity:

case R.id.photobtn:

                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
                    break;

  protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
               super.onActivityResult(requestCode, resultCode, data);

                      if(requestCode == CAMERA_REQUEST){      

                         if (resultCode==RESULT_OK){

                            Bitmap photo = (Bitmap) data.getExtras().get("data");
                            imageView.setImageBitmap(photo);
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            blobvalue = stream.toByteArray();

                             Bundle extras = new Bundle();
                             Intent k=new Intent(this,MainActivity.class);  
                             extras.putParcelable("Bitmap", photo);
                             k.putExtras(extras);
                         }
                      }

                      if (resultCode == RESULT_CANCELED) {    

                      }        


           }

In the adapter:

public View getView(int position, View convertView, ViewGroup arg2) {
            myItems theItems = getItems().get(position);

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.showadapterlist, null);          

            }



            ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);

            final Bitmap image;
            if(theItems.getImagemyItems() != null)
            {
                byte []temp = theItems.getImagemyItems();
                image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
                myImage.setImageBitmap(image);
            }
            else
            {
                image = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
                myImage.setImageBitmap(image);
            }

In myItems:

public class myItems {
     String id;
     ...
     byte[] imageblob;

     public String getID() {
      return id;
     }
     ....



         }
George
  • 5,808
  • 15
  • 83
  • 160
  • 6
    You usually never insert images/bitmaps into database. Place them on sd card and store their path in database. – M-Wajeeh Apr 11 '13 at 17:29
  • If you do store images in your database, they are stored as "Blobs". – HalR Apr 11 '13 at 18:27
  • I could be wrong, but the method you are trying to use is`setImage(String filename)`. You do not pass the image in string form, but a string of its location. – IAmGroot Apr 11 '13 at 18:40
  • @George Hi.., The first comment (M-WajeEh's comment) here will work for sure – Abhi Apr 19 '13 at 15:28
  • @Abhi:Hello ,i think I am trying to do this in my code ,isn't this right?(sorry i am not exprerienced..)Thanks! – George Apr 19 '13 at 15:30
  • @Abhi:Or can you tell me how to do this? – George Apr 19 '13 at 15:30
  • Come [here](http://chat.stackoverflow.com/rooms/5098/android-people) We will discuss there – Abhi Apr 19 '13 at 15:33
  • @Abhi:Hello, if you have time to help me , thanks! – George Apr 21 '13 at 17:33
  • For me it would be great if u could post somewhere the code because I have this same problem with IllegalStateException and Coursor and spended so much time with this problem. – MyWay Nov 13 '13 at 08:16
  • @MyWay:Ok,check here.http://pastebin.com/ghkYzD6W and here http://pastebin.com/zGUHQUp8 – George Nov 18 '13 at 18:03

1 Answers1

1

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....

kalandar
  • 793
  • 6
  • 13
  • now uninstall your previous application and try to run again... because the database already!!! so we need to remove that.... – kalandar Apr 24 '13 at 10:43
  • :Ok,now the app runs but I have no photo in listview of course.I am trying this " byte[] imageView=c1.getBlob(c1.getColumnIndex("imageblob")); ByteArrayInputStream inputStream = new ByteArrayInputStream(imageView); Bitmap theImage = BitmapFactory.decodeStream(inputStream);" but I am trying this " imageView.setImageBitmap(theImage);" and says "cannot invoke ..on the array type byte[]".Also , please check the myItems and myAdapter classes above.I am not sure what to put exactly.Thanks! – George Apr 24 '13 at 11:00
  • byte []temp = c.getBlob(3); Bitmap image = BitmapFactory.decodeByteArray(temp, 0, temp.length); BitmapDrawable dd = new BitmapDrawable(image.getImage()); imgView.setBackgroundDrawable(dd); try this instead of your code as well as check you created database file if the blob is stored or not... the db is there in data/data//databases/... – kalandar Apr 24 '13 at 11:04
  • :It says "the method getImage()"is undefined for the type Bitmap and "the method setBackgroundDrawable is deprecated " – George Apr 24 '13 at 11:09
  • byte []temp = c.getBlob(3); Bitmap image = BitmapFactory.decodeByteArray(temp, 0, temp.length); imageview.setImageBitmap(image); try this... and also check your db in that location.... – kalandar Apr 24 '13 at 11:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28796/discussion-between-george-and-kalandar) – George Apr 24 '13 at 11:14