Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓拍摄相机返回空意图_Android_Android Intent_Camera - Fatal编程技术网

Android 安卓拍摄相机返回空意图

Android 安卓拍摄相机返回空意图,android,android-intent,camera,Android,Android Intent,Camera,我正在开发android应用程序,我的应用程序有拍照按钮。之前,我陷入了一种状态,在拍摄照片后,onActivityResult中返回的数据为null。这是相机预期的行为,如果我们将额外的\u输出设置为intent,它将返回null。出于这个原因,我对代码进行了空检查,结果很好 几天后,我再次进行了测试。我还是再次陷入同样的问题。但是这次数据不是nulldata具有空的意图,例如intent和data。getData()变为null。我通过检查data来修复此问题。getData()==null

我正在开发android应用程序,我的应用程序有拍照按钮。之前,我陷入了一种状态,在拍摄照片后,onActivityResult中返回的数据为null。这是相机预期的行为,如果我们将
额外的\u输出设置为intent,它将返回
null
。出于这个原因,我对代码进行了空检查,结果很好

几天后,我再次进行了测试。我还是再次陷入同样的问题。但是这次
数据
不是
null
data
具有空的意图,例如
intent
data。getData()
变为
null
。我通过检查
data来修复此问题。getData()==null
再次工作。我不知道为什么会这样。只是好奇发生了什么。由于这个原因,我必须重新上传到生产-(

已编辑


我知道如何解决这个问题。我不明白的是,如果我输入额外的输出,返回的
data
必须是
null
。最重要的是,我几周前实现的代码,我非常确定
data
返回
null
,突然又变成了非
null
值。

不要使用
data.getData()
,改为使用
data.getExtras().get(“data”);
,查看问题是否再次出现。我不使用
data.getExtras()的原因
是因为如果我在intent中传递额外的输出,
数据
变成
null
。如果我没有错的话,这就是事实。谢谢@Rohit的回答,但我看不出我的代码有什么不同。你能指出为什么
intent
返回数据突然变成非null值吗?
Actually the camera intent doesnot return the data in intent because after getting image it kill the activity.

so try this


 void opencameraForPicture(int requestCode, Uri fileUri) {
        checkPermissionForMarshMello(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);


        Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        /* start activity for result pass intent as argument and request code */
        startActivityForResult(intent, requestCode);

    }

    /**
     * This method set the path for the captured image from camera for adding
     * the new  picture in the list
     */
    private Uri getOutputMediaFile() {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), "."
                + Constants.CONTAINER);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + System.currentTimeMillis() + ".png");
        Uri uri = null;
        if (mediaFile != null) {
            uri = Uri.fromFile(mediaFile);

        }
        return uri;
    }

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

        super.onActivityResult(requestCode, resultCode, data);

String imagePath = fileUri.getPath();

//you can decode this path as bitmap

}
Actually the camera intent doesnot return the data in intent because after getting image it kill the activity.

so try this


 void opencameraForPicture(int requestCode, Uri fileUri) {
        checkPermissionForMarshMello(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);


        Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        /* start activity for result pass intent as argument and request code */
        startActivityForResult(intent, requestCode);

    }

    /**
     * This method set the path for the captured image from camera for adding
     * the new  picture in the list
     */
    private Uri getOutputMediaFile() {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), "."
                + Constants.CONTAINER);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + System.currentTimeMillis() + ".png");
        Uri uri = null;
        if (mediaFile != null) {
            uri = Uri.fromFile(mediaFile);

        }
        return uri;
    }

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

        super.onActivityResult(requestCode, resultCode, data);

String imagePath = fileUri.getPath();

//you can decode this path as bitmap

}