如何保存从相机拍摄的图像并将其显示到列表视图 - 与" IllegalStateException"崩溃

时间:2013-04-11 17:14:30

标签: android sqlite listview

我对如何做到这一点很困惑。

对于我使用的数据库:

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

数据库助手:

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

myItems:

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

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

现在,我在MainActivity中有listview。从另一个活动ShowList,我有一个按钮用于拍摄照片:

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

在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);
                  }

我的适配器:

公共类myAdapter扩展了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;
    }

我的问题:

1)列表视图位于MainActivity中。照片取自ShowList活动。在ShowList活动中我有:

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

我应该如何处理“imageblob”值?

2)在MainActivity(列表视图所在)中,我有:

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

我该如何处理imageblob?

现在,当我启动我的应用程序时,在行

处出现“IllegalStateException”崩溃
  

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

。如果我尝试c1.setImage它不起作用。

------------------ NEW ---------------------------- ---------------------- 在一项活动中:

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) {    

                      }        


           }

在适配器中:

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

在myItems中:

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

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



         }

1 个答案:

答案 0 :(得分:1)

根据您的创建数据库声明

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

你被提及

 "+KEY_IMAGE+"  imageblob BLOB

所以列值是“imageblob imageblol BLOB”语法,它不是一个正确的sql语句。 所以只需从那里删除“imageblob”并尝试,它可能会解决您的问题......