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();,它應該開始工作。

相關問題