2014-11-04 187 views
0

我想借此與Android相機這樣的照片:拍攝照片,並顯示在ImageView的

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

this.startActivityForResult(intent, Globals.REQUEST_CODE_CAMERA) 

並將其存儲在一個ImageView的:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == Globals.REQUEST_CODE_CAMERA) { 
     if(resultCode == RESULT_OK) { 
      Bundle bundle = data.getExtras(); 
      Bitmap bitmap = (Bitmap) bundle.get("data"); 

      this.imageViewPhoto.setImageBitmap(bitmap); 
     } 
    } 
} 

我ImageView的配置像這樣:

<ImageView 
     android:id="@+id/nfcresult_imageview_photo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     android:adjustViewBounds="true" 
     android:clickable="true" 
     android:contentDescription="@string/imageview_photo_description" /> 

的所有作品,但是對ImageView的顯示的照片比小得多0相機拍攝的照片。我想要做的是在我的 ImageView中預覽一個小圖片,並在ImageView中添加一個OnClickListener來打開一個對話框 ,它顯示原始大小和分辨率的原始照片。它不能 難以做到這一點,但我實際上不能找出如何。

創建對話框並顯示照片我這樣做:

ImageView clone = new ImageView(this); 
clone.setImageBitmap(((BitmapDrawable)this.imageViewPhoto.getDrawable()).getBitmap()); 

DialogManager.showImageDialog(this, this.getResources().getString(R.string.title_photo), clone); 

的showImageDialog:

public static void showImageDialog(Context context, String title, ImageView imageView) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 

    builder.setTitle(title); 
    builder.setCancelable(false); 
    builder.setView(imageView); 
    builder.setPositiveButton(context.getResources().getString(R.string.button_back), new DialogInterface.OnClickListener() { 
     /** 
     * 
     */ 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
     } 
    }); 

    builder.create().show(); 
} 

對話框現在顯示存儲在ImageView的照片大小的照片 但我想顯示原始大小和原始分辨率的原始照片 ,但正如我已經說過的,ImageView應顯示原始 照片的較小版本。

我該如何做到這一點?

+0

http://androidexample.com/Camera_Photo_Capture_And_Show_Captured_Photo_On_Activity_/index.php?view=article_discription&aid=77&aaid=101 – 2014-11-04 07:41:02

+0

http://stackoverflow.com/questions/5991319/capture-image -from-camera-and-in-activity – 2014-11-04 07:43:01

+0

Thank you for the links ... but much deprecated stuff there ... – Mulgard 2014-11-04 08:17:39

回答

0

解決方案:

/** 
* 
*/ 
private void performAddPhoto() { 
    String timeStamp = Clock.getTimeStamp(); 
    String fileName = "Food_Shot_" + timeStamp + ".jpg"; 

    this.imagePath = Environment.getExternalStorageDirectory() + "/images/" + fileName; 

    File file = new File(this.imagePath); 
    file.getParentFile().mkdirs(); 

    try { 
     file.createNewFile(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

    startActivityForResult(intent, Globals.REQUEST_CODE_CAMERA); 
} 

/** 
* 
*/ 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == Globals.REQUEST_CODE_CAMERA) { 
     if(resultCode == RESULT_OK) { 
      this.onPhotoTaken(); 
     } 
    } 
} 

/** 
* 
*/ 
private void onPhotoTaken() { 
    this.imageTaken = true; 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 

    Bitmap bitmap = BitmapFactory.decodeFile(this.imagePath, options); 
    this.imageViewPhoto.setImageBitmap(bitmap); 
} 

/** 
* 
*/ 
private void performShowPhoto() { 
    ImageView imageView = new ImageView(this); 
    Bitmap bitmap = BitmapFactory.decodeFile(this.imagePath); 

    imageView.setImageBitmap(bitmap); 

    DialogManager.showImageDialog(this, this.getResources().getString(R.string.title_photo), imageView); 
} 
0

代替機器人:layout_width =「WRAP_CONTENT」設置ImageView的寬度和高度,以具體的DP值

<ImageView 
     android:id="@+id/nfcresult_imageview_photo" 
     android:layout_width="100dp" 
     android:layout_height="200dp" 
     android:src="@drawable/ic_launcher" 
     android:adjustViewBounds="true" 
     android:clickable="true" 
     android:contentDescription="@string/imageview_photo_description" /> 

保持高度和寬度的高寬比(而不是100和200,選擇合適的值)

+0

錯誤的wrap_content contant也是可行的 – 2014-11-04 07:41:43

0

你必須在你的意圖中額外添加一個名爲「MediaStore.EXTRA_OUTPUT」,你必須設置一個uri來存儲圖片。 你的回調函數被調用後,你可以從選擇的uri中加載圖片。 在這裏你可以得到完整分辨率的照片。

相關問題