相机意图 returns null onActivityResult

Camera Intent returns null onActivityResult

我正在尝试拍照并将其保存到自定义位置-

public void SavePhoto(View view){

    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "WorkingWithPhotosApp");
    imagesFolder.mkdirs();

    File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
    Uri uriSavedImage = Uri.fromFile(image);

    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(imageIntent, REQUEST_IMAGE_CAPTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(data==null){
        Toast.makeText(MainActivity.this, "Data is null", Toast.LENGTH_SHORT).show();
    }
    else{
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            ImageView mImageView=(ImageView)findViewById(R.id.imageView);
            mImageView.setImageBitmap(imageBitmap);
        }
    }
}

onActivityResult() 中的数据为空。我错过了什么?

像这样尝试:

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
       Strng imagePath = uriSavedImage.getPath(); // uriSavedImage - make it global
}

您在此处预插入一个 uri:

 imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

因此,当您获得 Activity.RESULT_OK 时,只需按其已知的 url 加载拍摄的照片。然后你可以像下面这样设置路径 onActivityResult 但你需要转换成 Bitmap.

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
           // Convert here your uri to bitmap then set it.//
            mImageView.setImageBitmap(YOUR_BITMAP);
 }

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "WorkingWithPhotosApp");

我认为您需要先检查您的目录是否存在。这就是我执行此任务的方式。在我的例子中,我在默认 DCIM 目录中创建了一个文件夹。

private void dispatchTakePictureIntent(int actionCode) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = null;
            try {
                f = setUpPhotoFile();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            } catch (IOException e) {
                e.printStackTrace();
                f = null;
            }
    startActivityForResult(takePictureIntent, actionCode);
    }

private File setUpPhotoFile() throws IOException {
        File f = createImageFile();
        return f;
    }

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "IMG_" + timeStamp + "_";
        File albumF = getAlbumDir();
        File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
        return imageF;
    }

private File getAlbumDir() {
        File storageDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            storageDir = getAlbumStorageDir(PHOTO_ALBUM_NAME);
            if (storageDir != null) {
                if (! storageDir.mkdirs()) {
                    if (! storageDir.exists()){
                        Log.d("Camera", "failed to create directory");
                        return null;
                    }
                }
            }
        } else {
            Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
        }
        return storageDir;
    }

public File getAlbumStorageDir(String albumName) {
        return new File (
                Environment.getExternalStorageDirectory()
                        + "/dcim/"
                        + albumName);
    }

希望对您有所帮助!

我和你有同样的问题,所以我决定实施给定的解决方案 here

然后,在我自己的项目中,我这样做了

首先 : 在manifest中创建provider,跟指南说的一样
其次:创建一个class名称的LegacyCompatCursorWrapper(ps。看指南或者联系我,我可以发代码),你需要这个class ,在后续步骤中。
第三个:创建这个classLegacyCompatFileProvider
第四: 做那个之前

startActivityForResult(takePictureIntent, actionCode);

实施这个

 Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);

    i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
        i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }
    else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
        ClipData clip=
                ClipData.newUri(getContentResolver(), "A photo", outputUri);

        i.setClipData(clip);
        i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }
    else {
        List<ResolveInfo> resInfoList=
                getPackageManager()
                        .queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);

        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            grantUriPermission(packageName, outputUri,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
    }

    try {
        startActivityForResult(i, CONTENT_REQUEST);
    }
    catch (ActivityNotFoundException e) {
        Toast.makeText(this, R.string.msg_no_camera, Toast.LENGTH_LONG).show();
        finish();
    }

所以在onActivityResult()

  if (requestCode == CONTENT_REQUEST) {
          //  if (resultCode == RESULT_OK) {
                Intent i=new Intent(Intent.ACTION_VIEW);
                Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);

                i.setDataAndType(outputUri, "image/jpeg");
                i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Bitmap thumbnail = null;
                Bundle extras = i.getExtras();
            try {
                thumbnail = MediaStore.Images.Media.getBitmap(this.getContentResolver(),outputUri );
            } catch (IOException e) {
                e.printStackTrace();
            }
            mImageProfile.setImageBitmap(thumbnail);
            saveImage(thumbnail);
 }

其中mImageProfile-->是View上的一个ImageView。 请记住,这是根据指南。