如何通过android中的相机意图预览捕获的图像

时间:2014-05-20 08:38:39

标签: android android-camera android-camera-intent

你好我通过相机意图捕捉图像它很好但我的问题是我没有得到该图像的预览 我的要求非常简单,通过相机点击照片后,它必须要求我保存或丢弃此图像,如果我按下SAVE然后它保存到SD卡中就可以了...

这是我的代码

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"是在SdCard中存储捕获图像的方法

2 个答案:

答案 0 :(得分: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中显示位图,并向用户显示“保存/删除”按钮。

答案 1 :(得分: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);
 }