2015-06-22 31 views
1

我想將一些圖像存儲在數據庫中,稍後恢復並在定製的ListView上顯示它們。我怎樣才能做到這一點?如何將圖像存儲在數據庫中並在列表視圖中顯示

這是我到目前爲止已經完成:

//here, we are making a folder named picFolder to store pics taken by the camera using this application 
dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/picFolder/"; 
    File newdir = new File(dir); 
    if(!newdir.exists()){ 
     newdir.mkdirs(); 
    } 


//Switch cases are 

情況下R.id.btnPhotoGallary2:

 Intent gallaryIntent = new Intent(Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(gallaryIntent , TAKE_GALLARY_CODE);//one can be replced with any action code 
     break; 

    case R.id.btnPhotoCamera1: 
     count++; 
     String file = dir+count+".jpg"; 
     File newfile = new File(file); 
     try { 
      newfile.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }  

     capturedPhotoUri = Uri.fromFile(newfile); 

     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedPhotoUri); 

     startActivityForResult(cameraIntent, TAKE_CAMERA_CODE); 
     break; 

//設置照片按鈕/ ImageView的

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case 0://Captured photo from Camera 
     if(resultCode == RESULT_OK){ 
      String pathNamePhoto = capturedPhotoUri.getPath().toString(); 
      System.out.println("Photo Path Is :"+pathNamePhoto); 
      d = BitmapDrawable.createFromPath(pathNamePhoto); 
      ivProfile.setImageDrawable(d); 
      Log.d("CameraDemo", "Pic saved"); 
      Log.v("CapturedPhoto PATH ==>> ",pathNamePhoto); 
      pd.setProfImagepath(pathNamePhoto); 
     } 
     dialog.dismiss(); 
     break; 


    case 1://Select Image from Gallery 
     if(resultCode == RESULT_OK){ 
      Uri selImageUri = data.getData(); 
      String pathNameImage = getPath(selImageUri); 
      System.out.println("Image Path Is :"+pathNameImage); 

      ivProfile.setImageURI(selImageUri); 
      Log.d("GallaryDemo", "Get Pic "); 
      Log.v("IMAGE PATH ==>> ",pathNameImage); 
      pd.setProfImagepath(pathNameImage); 
     }   
     dialog.dismiss(); 
     break; 
    } 

} 

//獲取圖像的真實路徑

@SuppressWarnings("deprecation") 
public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

回答

0

1.將圖像轉換或編碼爲base64字符串,並將其作爲字符串存儲在數據庫中。從數據庫中讀取時,再次將其解碼爲圖像。 2.將圖像轉換爲bytearray並將其存儲在數據庫中。

2

那麼,我在數據庫中存儲圖像的建議是:不要。 這不是有效的,最好使用路徑存儲它們。這在this question上進行了討論。

但是。如果你確實無論出於何種原因存儲它。嘗試將圖像編碼爲Base64 String,並將其存儲在數據庫中。

Android的Base64 class爲此。嘗試使用下面的代碼片段編碼:

Bitmap bitmap = BitmapFactory.decodeFile("image.jpg"); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP 
byte[] bai = baos.toByteArray(); 

String base64Image = Base64.encodeToString(bai, Base64.DEFAULT); 

// Call your method to save this string on the DB here. 

而且你必須對它進行解碼嘗試以下操作:

byte[] data = Base64.decode(base64Image, Base64.DEFAULT); 
Bitmap bm; 
BitmapFactory.Options opt = new BitmapFactory.Options(); 
opt.inMutable = true; 
bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt); 

// Now do whatever you want with the Bitmap. 

你可以看到文檔的hereBitmap類。

相關問題