2011-05-17 101 views
0

我是Android新手。我剛剛在Android 2.1中創建了一個帶有256 MB android-SDcard的AVD。我已經插入了兩個圖像。我已經使用DDMS的角度來做這件事。現在圖像存儲在SD卡的DCIM文件夾中的文件夾100ANDRO中。現在我想創建一個應用程序,允許用戶通過瀏覽文件夾來選擇圖像,並需要將相應的圖像保存到數據庫android-sqlite中。如何將android中的SD卡中的圖像存儲到數據庫中?

有人可以幫我找到適當的方法嗎?提前致謝。

回答

1

我找到了一種方法。

我已經創建了一個按鈕,用於UPLOAD和點擊操作,我已經設置爲這樣。

upload.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) { 
      Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, 1); 

     } 
    }); 

而且我已經重寫了這個方法以及下面的類。

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case 1: 
    { 
     if (resultCode == RESULT_OK) 
     { 
     Uri photoUri = data.getData(); 
     if (photoUri != null) 
     { 
     try { 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    String filePath = cursor.getString(columnIndex); 
    cursor.close(); 
    Bitmap bitmap = BitmapFactory.decodeFile(filePath); 
    imgView.setImageBitmap(bitmap); 
    int size = bitmap.getWidth() * bitmap.getHeight(); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(size); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
    try { 
    out.flush(); 
    out.close(); 
    } catch (IOException e) { 
    e.printStackTrace();} 
    String bb = out.toString(); 
    byte[] x = out.toByteArray(); 
    image_value.setTag(x); 
    image_value.setText(filePath); 
    }catch(Exception e) 
     {} 
     } 
    } 
    } 

image_value表示xml文件中的隱藏文本視圖。 我已經將圖像位置和字節的值作爲文本視圖的值和標記傳遞。 後來我將這些字節保存到數據庫中供以後顯示。它的工作正常。

感謝所有。

相關問題