Android Capturing an image From Camera Returns Empty Data [duplicate]

走远了吗. 提交于 2020-01-16 18:06:52

问题


While Capturing image in android app intent return null each time. Please see below the code which I am using. I have tried multiple ways to get the permission as well as intent set but still I am unable to get data.

Action For Camera

 private void takePhotoFromCamera() {

    if(ActivityCompat.checkSelfPermission(ObservationsView.this, 
    Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(getExternalCacheDir(),
                String.valueOf(System.currentTimeMillis()) + ".jpg");fileUri = Uri.fromFile(file);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }else {
    String[] permissionRequest = {Manifest.permission.CAMERA};
    requestPermissions(permissionRequest, 8675309);
    checkPermission();
    }
}

And Here is the Response Code

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == RESULT_OK ) {
        Uri selectedImg = null;
        Bitmap bmp = null;
        Bundle extras;
        if (requestCode == CAMERA_REQUEST) {
            selectedImg = data.getData();
            extras = data.getExtras();

            bmp = (Bitmap) extras.get("data");

        }

        else if (requestCode == CAMERA_REQUEST) {

            selectedImg = fileUri;


            // Bitmap mphoto = (Bitmap) data.getExtras().get("data");
            try {
                bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
                if (bmp != null) {
                    bmp = getBitmapFromUri(fileUri);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }

            Uri tempUri = getImageUri(getApplicationContext(), bmp);


            Intent passPhoto = new Intent(this, Photo.class);
            passPhoto.putExtra("image",tempUri);

            passPhoto.putExtra("Caller", getIntent().getComponent().getClassName());
            startActivity(passPhoto);


        }

回答1:


Try to use the following code for getting the image in all APIs -

 public void takePicture() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            file = FileProvider.getUriForFile(mContext,
                    BuildConfig.APPLICATION_ID + ".provider",
                    getOutputMediaFile());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
            startActivityForResult(intent, TAKE_IMAGE_REQUEST);
        }

 private File getOutputMediaFile() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), getString(R.string.app_folder_name));

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }


        return new File(mediaStorageDir.getPath() + File.separator +
                PROFILE_PIC + getString(R.string.pic_type));
    }

And on your onActivityResult -

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_IMAGE_REQUEST:
                if (resultCode == RESULT_OK) {
                    imageView.setImageURI(file);
                }
                break;

            default:
                break;

        }
    }

Also, you need to define the Provider in your Manifest.xml file -

  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

And finally the provider_paths.xml will be as follows -

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

Let me know in case you need any clarification.



来源:https://stackoverflow.com/questions/53113646/android-capturing-an-image-from-camera-returns-empty-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!