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

2023-09-12 03:46:54 作者:咸鱼

我感到困惑的是如何做到这一点。

有关数据库的使用:

 公共无效insertImage(byte []的字节){
         ContentValues​​ CV =新ContentValues​​();

         cv.put(imageblob,字节);
         Log.e(插入,插入);
         。dbHelper.getWritableDatabase()插入(图像,imageblob,CV);

     }

     公众的byte []的getImage(光标C){
         返程(c.getBlob(1));
     }
 

数据库助手:

  ...
 公共静态最后弦乐KEY_COMMENTS =意见;
公共静态最后弦乐KEY_IMAGE =imageblob;

私有静态最后弦乐SCRIPT_CREATE_DATABASE =创建表
 + DATABASE_TABLE +(+ KEY_ROWID
   +整数主键自动增量,+ KEY_TITLE
  +文本不为空,...... + KEY_COMMENTS +文字不为空,+ KEY_IMAGE +imageblob BLOB);;
 
无惧挑战的便携相机 徕卡Q2

在myItems:

 公共类myItems {
     ....
     字符串的意见;
 byte []的imageblob;
      公众的byte []的getImage(){
      返回imageblob;
     }

 公共无效setImage(byte []的imageblob){
      this.imageblob = imageblob;
    }
 

现在,我的列表视图中MainActivity.From另一个活动,ShowList,我与它拍照的按钮:

 案例R.id.photobtn:
    意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
    startActivityForResult(cameraIntent,CAMERA_REQUEST);
    打破;
 

在onActivityResult:

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){

                  如果(结果code == CAMERA_REQUEST){
                        。位图照片=(位图)data.getExtras()获得(数据);
                        imageView.setImageBitmap(照片);
                        ByteArrayOutputStream流=新ByteArrayOutputStream();
                        photo.com preSS(Bitmap.Com pressFormat.PNG,100,流);
                        字节[]的字节数组= stream.toByteArray();

                        sqlHandler.insertImage(字节阵列);
                  }
 

我的适配器:

公共类myAdapter扩展了BaseAdapter {

  ...

    公共myAdapter(上下文的背景下,ArrayList的< myItems>列表){
        this.context =背景;
        this.setItems(名单);
    }

        @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中ARG2){
        。myItems theItems = getItems()获得(位置);

        如果(convertView == NULL){
            LayoutInflater充气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.showadapterlist,NULL);

        }

           ...
           TextView的评论=(TextView中)convertView.findViewById(R.id.text_comments);
        Comments.setText(theItems.getComments());

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

           返回convertView;
          }

    公共静态位图getImageFromBLOB(byte []的mBlob)
      {
        byte []的BB = mBlob;
        返回BitmapFactory.de codeByteArray(BB,0,bb.length);
     }

    公众的ArrayList< myItems> getItems(){
        返回的项目;
    }

    公共无效setItems(ArrayList中< myItems>项目){
        this.items =项目;
    }
 

我的问题:

1)ListView控件位于MainActivity.The照片取自ShowList activity.In ShowList活动,我有:

 案例R.id.OKbtn:

字符串mycomments = comments.getText()的toString()。
byte []的myimageblob = NULL;
 查询字符串=INSERT INTO MEMOR(标题,...评论,imageblob)+
值(.. + mytitle +... + mycomments +,+ myimageblob +')sqlHandler.executeQuery(查询);

  意图K =新的意图(这一点,MainActivity.class);
  的setResult(RESULT_OK中,k);
   完();
 

我应该如何处理imageblob值?

2)在MainActivity(如列表视图是)我有:

  ...
    myItems theItems =新myItems();
                                 theItems.setID(c1.getString(c1.getColumnIndex(_ ID)));
                             theItems.setTitle(c1.getString(c1.getColumnIndex(标题)));

      ...
                                 theItems.setComments(c1.getString(c1.getColumnIndex(意见)));
  byte []的BLOB = c1.getBlob(c1.getColumnIndex(imageblob));
   ByteArrayInputStream进行的InputStream =新ByteArrayInputStream的(BLOB);
   点阵位图= BitmapFactory.de codeStream(InputStream的);

   myList.add(theItems);
 

我应该如何处理imageblob?

现在,当我开始我的应用程序崩溃与IllegalStateException异常的行

  

的byte [] BLOB = c1.getBlob(c1.getColumnIndex(imageblob));

。如果我尝试c1.setImage这是行不通的。

------------------新---------------------------- ---------------------- 在一个活动:

 案例R.id.photobtn:

                    意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
                    startActivityForResult(cameraIntent,CAMERA_REQUEST);
                    打破;

  保护无效onActivityResult(INT申请code,INT结果code,意图数据){
               super.onActivityResult(要求code,因此code,数据);

                      如果(要求code == CAMERA_REQUEST){

                         如果(结果code == RESULT_OK){

                            。位图照片=(位图)data.getExtras()获得(数据);
                            imageView.setImageBitmap(照片);
                            ByteArrayOutputStream流=新ByteArrayOutputStream();
                            photo.com preSS(Bitmap.Com pressFormat.PNG,100,流);
                            blobvalue = stream.toByteArray();

                             捆绑额外=新包();
                             意图K =新的意图(这一点,MainActivity.class);
                             extras.putParcelable(位图,照片);
                             k.putExtras(临时演员);
                         }
                      }

                      如果(结果code == RESULT_CANCELED){

                      }


           }
 

在适配器:

 公开查看getView(INT位置,查看convertView,ViewGroup中ARG2){
            。myItems theItems = getItems()获得(位置);

            如果(convertView == NULL){
                LayoutInflater充气=(LayoutInflater)上下文
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.showadapterlist,NULL);

            }



            ImageView的MYIMAGE =(ImageView的)convertView.findViewById(R.id.myimage);

            最终的位图图像;
            如果(theItems.getImagemyItems()!= NULL)
            {
                byte []的温度= theItems.getImagemyItems();
                图像= BitmapFactory.de codeByteArray(温度,0,temp.length);
                myImage.setImageBitmap(图像);
            }
            其他
            {
                图像= BitmapFactory.de codeResource(context.getResources(),R.drawable.ic_launcher);
                myImage.setImageBitmap(图像);
            }
 

在myItems:

 公共类myItems {
     字符串ID;
     ...
     byte []的imageblob;

     公共字符串的getID(){
      返回ID;
     }
     ....



         }
 

解决方案

根据创建数据库语句

 私有静态最后弦乐SCRIPT_CREATE_DATABASE =创建表
 + DATABASE_TABLE +(+ KEY_ROWID
 +整数主键自动增量,+ KEY_TITLE
 +文本不为空,...... + KEY_COMMENTS +文字不为空,+ KEY_IMAGE +imageblob BLOB);;
 

你提到的

 + KEY_IMAGE +imageblob BLOB
 

所以列值是imageblob imageblol BLOB语法明智它不是一个正确的SQL语句。 所以只是从那里取出imageblob和尝试,它可能会解决你的问题......

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



         }

解决方案

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