【问题标题】:How to get preview of captured image through camera intent in android如何通过android中的相机意图预览捕获的图像
【发布时间】:2014-05-20 08:38:38
【问题描述】:

您好,我正在通过相机意图捕捉图像,效果很好,但我的问题是没有获得该图像的预览 我的要求很简单,通过相机点击照片后,它必须要求我保存或丢弃这张图片,如果我按 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 中的方法

【问题讨论】:

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


    【解决方案1】:

    您可以像这样预览捕获的文件:

    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 中显示位图并向用户显示保存/删除按钮。

    【讨论】:

    【解决方案2】:

    您必须从代码中删除行。

     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);
     }
    

    【讨论】:

    • 如果 (resultCode != RESULT_OK) 返回,我删除了;这行但不符合我的要求:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2011-03-23
    • 2012-07-19
    • 2019-04-13
    • 1970-01-01
    • 2013-08-26
    相关资源
    最近更新 更多