2012-07-04 36 views
0

我正在为客户创建应用程序存储他们的图像和详细信息。我正在通过相机或SD卡浏览图像。我通过sdcard获取ImageView中的图像,但是当我将它插入数据库时​​,它不插入数据库中的图像列。我从SD卡获取图像并在imageview上显示它现在我想将此图像插入数据库,但未将其存储在数据库中

我点击SD卡按钮浏览图像。现在这个图像显示在ImageView

Button btnsdcard = (Button) findViewById(R.id.custbtnimage); 
    btnsdcard.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(
        Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 
     } 
    }); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case 1: 
     if (resultCode == RESULT_OK) { 
      Uri selectedImage = data.getData(); 
      try { 
       InputStream imgstream = getContentResolver() 
         .openInputStream(selectedImage); 
       Bitmap btm = BitmapFactory.decodeStream(imgstream); 
       ImageView img = (ImageView) findViewById(R.id.imagecust); 
       img.setImageBitmap(btm); 
       ByteArrayOutputStream bo = new ByteArrayOutputStream(); 
       btm.compress(Bitmap.CompressFormat.PNG, 100, bo); 
       byte[] custimage = bo.toByteArray(); 
      } catch (FileNotFoundException e) {     
       e.printStackTrace(); 
       Toast.makeText(getBaseContext(), "file not fount", 
         Toast.LENGTH_SHORT).show(); 
      } 
    } } 
    } 

现在当我点击保存按钮其他数据存储在数据库中但图像列在数据库中为空,我没有得到任何错误。

btnsave.setOnClickListener(new OnClickListener() { 

     @SuppressLint("ParserError") 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String name = custname.getText().toString(); 
      String address = custaddress.getText().toString(); 
      Long pincode = Long.parseLong(custpincode.getText().toString()); 
     String city = custcity.getText().toString(); 


      byte[] image=custimage; 

      Customerprofile profile = new Customerprofile(); 
      profile.setName(name); 
      profile.setAddress(address); 
      profile.setPincode(pincode); 
      profile.setCity(city); 
      profile.setImage(image); 

      insertcust(profile); 
         } 

     private void insertcust(Customerprofile profile) { 
      // TODO Auto-generated method stub 
      DatabaseHelper mydbhelper = new DatabaseHelper(Myimage.this); 
      SQLiteDatabase db = mydbhelper.getWritableDatabase(); 
      ContentValues values = new ContentValues(); 
      values.put(DatabaseHelper.KEY_NAME, profile.getName()); 
      values.put(DatabaseHelper.KEY_ADDRESS, profile.getAddress()); 
      values.put(DatabaseHelper.KEY_PINCODE, profile.getPincode()); 
      values.put(DatabaseHelper.KEY_CITY, profile.getCity()); 
      values.put(DatabaseHelper.KEY_IMAGE, profile.getImage()); 

      long adcid = db.insert(DatabaseHelper.DATABASE_TABLE, null, 
        values); 

      db.close(); 


     } 
    }); 
} 

谁能帮我给我的解决方案存储在数据库中我ImageView图像。

回答

0

您确实应该将图像存储在数据库以外的其他位置,并将图像的路径放在数据库中。

如果你真的想把它放在数据库中,你需要创建一个包含BLOB类型的列来存储它(因为你没有显示你的数据库结构,所以我提到这个)。

然后,您需要将图像转换为字节数组,然后将其保存到数据库。纵观你的代码,它看起来像你正在做的一切......

发现你的问题。您正在使用本地变量,然后尝试在范围之外引用它们。您的onActivityResult中有byte[] custimage = bo.toByteArray();,然后在您的onClick超出范围时尝试使用它byte[] image=custimage;

申报custimage作为一类领域,使所有的方法都可以访问它byte[] custimage;,改变onActivityResult的发生,以custimage = bo.toByteArray();,它应该开始工作。

相关问题