简体   繁体   English

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

[英]How to get preview of captured image through camera intent in android

Hi am capturing an image through camera intent it works good but my problem is that am not getting preview of that image my requirement is very simple, after clicking of photo through camera it must ask me to SAVE or DISCARD this image if i press SAVE then it get save into sd card thats it... 大家好,我是通过摄像头意图捕获图像的,但是效果很好,但我的问题是无法获取该图像的预览,我的要求非常简单,在单击通过摄像头的照片后,如果我按SAVE,则必须让我保存或删除该图像它保存到SD卡中就是这样...

here is my code 这是我的代码

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;

here "iu" is object of ImageUtility class and "SaveCapturedImage" is method to store that captured image in SdCard 这里的“ iu”是ImageUtility类的对象,“ SaveCapturedImage”是将捕获的图像存储在SdCard中的方法

You can get a preview of the captured File as this: 您可以通过以下方式预览捕获的文件:

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

then you can show the Bitmap in an ImageView and present the user with Save/Delete buttons. 然后您可以在ImageView中显示位图,并向用户显示“保存/删除”按钮。

You have to remove line from your code. 您必须从代码中删除行。

 if (resultCode != RESULT_OK)
    return;

Use following code : 使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM