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

Intent only getting images from the back camera

我的应用程序应该从 图库 中获取图片并将其显示在 ImageView 中,除了那些图片之外,我得到了我想要的所有图片我用 后置摄像头 拍摄,它们出现在画廊中供我选择,甚至 return 路径,但我得到的只是 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();
        }
    }
}

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

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

原因是我认为索引内存不足。 请参考这个 link Compress camera image before upload 你可以理解

即使压缩后无法获取,也可以尝试使用一些库从图库中挑选图像。

问题出在图像的大小上。

我已经成功解决了在 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");
}