android – Camera Result总是返回RESULT_CANCELED

前端之家收集整理的这篇文章主要介绍了android – Camera Result总是返回RESULT_CANCELED前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用相机拍照.我只是暂时需要它们所以我做以下事情:
private void takePicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (intent.resolveActivity(this.getActivity().getPackageManager()) != null) {
        try {
            this.imageFile = File.createTempFile("CROP_SHOT",".jpg",this.getActivity().getCacheDir());

            intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(this.imageFile));
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);

            this.startActivityForResult(intent,Globals.REQUEST_TAKE_PHOTO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

不幸的是,我的onActivityResult中的结果代码为RESULT_CANCELED

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
    Log.v("PictureEdit","onActivityResult");

    super.onActivityResult(requestCode,resultCode,data);

    if(resultCode == Activity.RESULT_OK) {
        if(requestCode == Globals.REQUEST_TAKE_PHOTO) {
            Log.v("PictureEdit","onActivityResult take photo");

            this.startCropImage(Uri.fromFile(this.imageFile));
        } else if (requestCode == Globals.REQUEST_PICK_PHOTO) {
            Log.v("PictureEdit","onActivityResult pick photo");

            this.startCropImage(data.getData());
        } else if (requestCode == Globals.REQUEST_CROP_PHOTO) {
            Log.v("PictureEdit","onActivityResult crop photo");

            this.imageEncoded = data.getStringExtra(Globals.KEY_IMAGE_ENCODED);

            this.imageView.setImageBitmap(Images.decodeImage(imageEncoded));
            this.buttonSave.setEnabled(true);
        } else {
            Log.v("PictureEdit","onActivityResult requestCode: " + requestCode + ",result: " + resultCode);
        }
    } else {
        Log.v("PictureEdit","onActivityResult resultCode is not ok: " + resultCode);
    }
}

我到目前为止在stackoverflow和网络上找到的每个解决方案都没有解决我的问题.我使用缓存目录来创建临时文件

File.createTempFile("CROP_SHOT",this.getActivity().getCacheDir());

我有正确的权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

从我的智能手机中挑选图像总能正常工作:

private void pickPicture() {
    Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    this.startActivityForResult(intent,Globals.REQUEST_PICK_PHOTO);
}

那么为什么相机总是返回RESULT_CANCELED?

它出现在Android 6.0的Nexus 4和Android 4.2的三星Galaxy S III上.

解决方法

I use the cache directory for creating a temp file:

首先,第三方相机应用程序无权在internal storage上使用应用程序的专用缓存目录.

其次,您不应该创建该文件.相机应用程序应该创建该文件.某些相机应用可能会混淆并拒绝覆盖您的文件.

因此,创建指向external storage上某个位置的File对象(例如,getExternalCacheDir()),但不要创建该文件.使用该File对象作为EXTRA_OUTPUT值的基础.

另请注意,MediaStore.EXTRA_VIDEO_QUALITY用于录制视频;它对ACTION_IMAGE_CAPTURE毫无意义.

原文链接:https://www.f2er.com/android/315695.html

猜你在找的Android相关文章