android - Intent 仅从后置摄像头获取图像

标签 android android-intent android-camera android-image

我的应用程序应该从 图库 获取图片并将其显示在 ImageView 中,除了我拍摄的图片之外,我得到了我想要的所有图片使用我的后置摄像头,它们出现在画廊中供我选择甚至返回路径,但我得到的只是ImageView 中的空白。

这是代码:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent, SELECT_PICTURE);

我的 onActivityResult 代码是这样的:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK && null != data) {
        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
            ImageView iv = (ImageView) getView().findViewById(R.id.iv_foto);
            iv.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

它适用于我的前置摄像头拍摄的图像。

我还没有在其他手机上尝试过该应用程序,但如果在我的手机上发生这种情况,很可能其他人也会遇到同样的问题。

最佳答案

问题在于图像的大小。

我已经成功解决了在 ImageView 中显示之前缩放 Bitmap 的相同问题

Bitmap sourceBitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri);

try {
        Bitmap sourceBitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri);

        float rotationInDegrees = 0;

        Cursor cursor = contentResolver.query(uri, new String[]{MediaStore.Images.ImageColumns.ORIENTATION},
                null,
                null,
                null);

        if (cursor != null && cursor.moveToFirst()) {
            int col = cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION);
            if (col != -1)
                rotationInDegrees = cursor.getInt(col);
            cursor.close();
        }

        Matrix matrix = new Matrix();
        matrix.preRotate(rotationInDegrees);

        int width, height;
        double aspectRatio;
        aspectRatio = (double) sourceBitmap.getWidth() / sourceBitmap.getHeight();
        if (sourceBitmap.getHeight() > sourceBitmap.getWidth()) {
            height = MAX_IMAGE_DIMENSION;
            width = (int) (height * aspectRatio);
        } else {
            width = MAX_IMAGE_DIMENSION;
            height = (int) (width / aspectRatio);
        }
        sourceBitmap = Bitmap.createScaledBitmap(sourceBitmap, width, height, false);

        return Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, false);

} catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("ImageHelper@getImageFromUri: IOException");
}

关于android - Intent 仅从后置摄像头获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484497/

相关文章:

android - 我设置了uses-feature android :required ="false" but Google play keeps insisting on these features

android - 为什么不推荐使用 TabListener?

android - 使用相机录制时抛出异常

Android - 以编程方式检查开发人员选项?

android - 从Android Kotlin中的数据库获取值为真的行数

android - 使用 MediaCodec,如何生成间隔小于 1 秒的关键 i 帧?

java - 尝试通过电子邮件发送文件

android - 在android中打开通知时页面不刷新

android - Google Play 音乐不再支持 media_play_from_search

java - "Failure delivering result",由于尝试图像捕获时 Motorola Bravo 上的 ManagedQuery 中出现 NPE。在 Evo 上运行良好