在android中从摄像头捕获图像后,图像不显示在活动内部

时间:2014-11-26 10:14:21

标签: android

我编写了一个程序,其中有一个按钮,单击该按钮可以通过摄像头捕获照片,并希望将捕获的图像设置在按钮下方的相同活动上。

一切正常而不会出错。图像也会保存到相应的位置。但是图像没有显示出来,这意味着出现了问题。

screenshot of my application in which below the button I want the captured image to be displayed

以下是我的上述代码:

    public class MainActivity extends ActionBarActivity {

    Button b1;
    private File imageFile;
    ImageView img;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.buttonPicture);
        img = (ImageView) findViewById(R.id.imageView1);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                imageFile = new File(
                        Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                        "test.jpeg");
                uri = Uri.fromFile(imageFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                startActivityForResult(intent, 0);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        if (requestCode == 0 && data != null) {
            switch (resultCode) {
            case Activity.RESULT_OK:
                if (imageFile.exists()) {
                    Bitmap photo = (Bitmap) data.getExtras().get(
                            MediaStore.EXTRA_OUTPUT);
                    previewCapturedImage();

                    img.setImageBitmap(photo);
                } else {
                    Toast.makeText(getBaseContext(), "File was not saved",
                            Toast.LENGTH_SHORT).show();
                }
                break;
            case Activity.RESULT_CANCELED:
                break;
            default:
                break;
            }
        }
    }

    private void previewCapturedImage() {
        try {

            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            options.inSampleSize = 8;

            final Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath(),
                    options);

            img.setImageBitmap(bitmap);

        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在捕获图像后按Ok按钮后,您可以使用通过Extras收到的Intent(您的案例中的数据)参数从onActivityResult获取捕获的图像 只需在onActivityResult

中使用此代码即可
Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                    imgViewLogo.setImageBitmap(photo);

答案 1 :(得分:0)

尝试使用以下代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        if (requestCode == 0 && data != null) {
            switch (resultCode) {
            case Activity.RESULT_OK:
                if (imageFile.exists()) {
                     private String selectedImagePath;
            Uri selectedImageUri = data.getData();
            String filePath = null;
            String filemanagerstring = selectedImageUri.getPath();

            selectedImagePath = getPath(selectedImageUri);
                        if (selectedImagePath != null) {
                            filePath = selectedImagePath;
                        } else if (filemanagerstring != null) {
                            filePath = filemanagerstring;
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.unknownPath, Toast.LENGTH_LONG).show();

             if (filePath != null) {
                            decodeFile(filePath);
                        } else {
                            bitmap = null;
                        }
                } else {
                    Toast.makeText(getBaseContext(), "File was not saved",
                            Toast.LENGTH_SHORT).show();
                }
                break;
            case Activity.RESULT_CANCELED:
                break;
            default:
                break;
            }
        }
    }

    //getPath method

    public String getPath(Uri uri) {
            String[] projection = { MediaColumns.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }

    //decodeFile method

    public void decodeFile(String filePath) {
    private Bitmap bitmap;
            try {

                File f = new File(filePath);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    angle = 90;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    angle = 180;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    angle = 270;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;

                Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
                ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstudentstreamOutputStream);
                img.setImageBitmap(selfieBitmap);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }