2014-05-20 20 views
0

中捕獲圖像的預覽通過攝像機意圖捕獲圖像它的作品不錯,但我的問題是,我沒有得到該圖像的預覽 我的要求非常簡單,點擊照片後相機必須問我保存或如果我按SAVE然後得到它保存到SD卡完蛋了放棄此圖像...如何通過攝像機意圖在android

這裏是我的代碼

private void openCamera() { 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    capturedFile = new File(Environment.getExternalStorageDirectory(), 
      "tmp_nookster_profilepic" 
        + String.valueOf(System.currentTimeMillis()) + ".jpg"); 

    mImageCaptureUri = Uri.fromFile(capturedFile); 

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
      mImageCaptureUri); 

    try { 
     intent.putExtra("return-data", true); 

     startActivityForResult(intent, PICK_FROM_CAMERA_NO_CROP); 
    } catch (ActivityNotFoundException e) { 
     e.printStackTrace(); 
    } 








protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_OK) 
     return; 

    switch (requestCode) { 
    case PICK_FROM_CAMERA_NO_CROP: { 
      iu.SaveCapturedImage(BitmapFactory.decodeFile(capturedFile 
        .getAbsolutePath())); 
      try { 
       if (capturedFile.exists()) 
        capturedFile.delete(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

     break; 

這裏「IU」是ImageUtility的對象類和「SaveCapturedImage」是將捕獲的圖像存儲在SD卡中的方法

回答

0

你可以得到捕捉文件的預覽,因爲這:

Bitmap getPreview(File image) { 
    final int THUMBNAIL_SIZE = 72; 
    BitmapFactory.Options bounds = new BitmapFactory.Options(); 
    bounds.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(image.getPath(), bounds); 
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) 
     return null; 

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight 
      : bounds.outWidth; 

    BitmapFactory.Options opts = new BitmapFactory.Options(); 
    opts.inSampleSize = originalSize/THUMBNAIL_SIZE; 
    return BitmapFactory.decodeFile(image.getPath(), opts); 
} 

那麼你就可以顯示在ImageView的位圖,並保存/刪除按鈕呈現給用戶。

+0

WIL它顯示本地相機 –

+0

的inbuild預覽選項,我不知道你是如何開始的相機意圖 - 檢查這個答案http://stackoverflow.com/a/5071133/72746 – Axarydax

0

你必須從你的代碼中刪除行。

if (resultCode != RESULT_OK) 
    return; 

使用下面的代碼:

if (resultCode == RESULT_OK) 
    { 
      try 
      { 
       Bitmap bitmap=null; 
       String imageId = convertImageUriToFile(imageUri,AddRecipes.this);  
       bitmap = BitmapFactory.decodeFile(imageId); 
}} 


public static String convertImageUriToFile (Uri contentUri, Activity activity) 
{ 
    String[] proj = { MediaStore.Images.Media.DATA };   
     CursorLoader cursorLoader = new CursorLoader(activity.getApplicationContext(),contentUri, proj, null, null, null);   
     Cursor cursor = cursorLoader.loadInBackground();   
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
} 
+0

我刪除if(resultCode!= RESULT_OK) return;這行,但它不符合我的要求:( –

相關問題