问题是从Camera捕获图像以作为ImageView对象查看。 -Android

问题描述 投票:0回答:2

我想创建一个应用程序,当用户单击按钮时,它会将您带到手机的默认相机应用程序,捕获图片并将其带回到活动的imageview对象中,以在活动中显示。下面是我的代码。

问题:问题在于图像未显示在图像视图中。拍照后返回活动,但不显示。

AndroidManifest.xml添加了以下内容:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera" android:required="true" />
...

   <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.application.hidden.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-files-path name="my_images" path="/" />
</paths>

MainActivity.java

private File createImageFile() throws IOException {
        String timeStamp =
                new SimpleDateFormat("yyyyMMdd_HHmmss",
                        Locale.getDefault()).format(new Date());
        String imageFileName = "IMG_" + timeStamp + "_";
        File storageDir = this.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,
                ".jpg",
                storageDir
        );

        imagePath = image.getAbsolutePath();
        return image;
    }

@OnClick({R.id.takePicture, R.id.pic})
    void onTakePic() {
        changeImageLayout.setVisibility(GONE);
        System.out.println("Setting up for capture, clicked on camera");
        Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(pictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            //Create a file to store the image
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
            }
            if(photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this.getContext(),"com.application.hidden.provider", photoFile);
                pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(pictureIntent, RESULT_LOAD_CAMERA_IMG);
            }
        }
    }

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            case RESULT_LOAD_CAMERA_IMG:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        Glide.with(this).load(imagePath).into(listingImage);
                        break;
                    case Activity.RESULT_CANCELED:
                        isNewImageAdded = false;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

非常感谢任何建议或帮助!

谢谢。

android android-intent android-camera android-imageview android-fileprovider
2个回答
0
投票
您需要将文件路径转换为文件对象,使用新的File(imagepath),而不仅仅是下滑道内的imagepath。

0
投票
我认为您正在尝试从相机捕获图像并将其存储在本地内存中,然后从内存中加载。

如果只想在imageview中显示捕获的图像,请按照以下方式进行。

对于打开的相机:

intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 7);

然后在onActivityResult结果中:

if (requestCode == 7 && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(bitmap); }

© www.soinside.com 2019 - 2024. All rights reserved.