相机意图在onActivityResult上为null

时间:2014-12-05 14:23:53

标签: android android-camera-intent onactivityresult

我正在研究Android应用程序,其中我从图库和相机获取图像。对于图库图像我在onActivityresult上获得了完美的意图值,但是当我想为Uri获取intent.getData()时,它会给出Null值。

我的代码如下:

public void onClick(DialogInterface dialog, int item) {

                         if(item==1){
                         Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent,
                                    "Select Picture"), SELECT_PICTURE);
                     }if(item==0){

                            dispatchTakePictureIntent();
                        // open();
                     }else{
                         alert.cancel();
                     }
                     alert.cancel();
                     }
                   });


private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            // Uri selectedImageUri1 = data.getData();
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                if (Build.VERSION.SDK_INT < 19) {
                    selectedImagePath = getPath(selectedImageUri);

                    Picasso.with(this).load(selectedImageUri).resize(100, 100).transform(new CircleTransform() {
                        @Override
                        public Bitmap transform(Bitmap source) {
                            int size = Math.min(source.getWidth(), source.getHeight());

                            int x = (source.getWidth() - size) / 2;
                            int y = (source.getHeight() - size) / 2;

                            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
                            if (squaredBitmap != source) {
                                source.recycle();
                            }

                            Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

                            Canvas canvas = new Canvas(bitmap);
                            Paint paint = new Paint();
                            BitmapShader shader = new BitmapShader(squaredBitmap,
                                    BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
                            paint.setShader(shader);
                            paint.setAntiAlias(true);

                            float r = size / 2f;
                            canvas.drawCircle(r, r, r, paint);

                            squaredBitmap.recycle();

                            return bitmap;
                        }
                    }).into(image);

                }
            }
            if (requestCode == REQUEST_IMAGE_CAPTURE) {
                Bundle extras = data.getExtras();
                Uri selectedImageUri = data.getData();
                // data.getData() gives null value
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                image.setImageBitmap(imageBitmap);

            }
}

2 个答案:

答案 0 :(得分:2)

使用相机意图时需要 Providers

file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

相机权限:

 private void askCameraPermissions() {
        if(ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.CAMERA}, CAMERA_PERM_CODE);
        } 
    }

相机意图:

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, SELECT_PICTURE);
            }
        }

清单:

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />

        </provider>

答案 1 :(得分:0)

AFAIK,你可以使用Intent数据只有缩略图或类似的东西,而且有些设备根本不会返回缩略图。要拍摄并获取照片,您可以参考this