Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:android 7.0 相机权限问题FILEPROVIDER #127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jiangm93
Copy link

android 7.0 手机点击照相崩溃

@zjianflys
Copy link

希望楼主更新一下,API>23过后会报错

@huizai0613
Copy link

/**
* 选择相机
*/
private void showCameraAction()
{
// 跳转到系统照相机
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// 设置系统相机拍照后的输出路径
// 创建临时文件
try {
mTmpFile = FileUtils.createTmpFile(getActivity());
} catch (IOException e) {
e.printStackTrace();
}
if (mTmpFile != null && mTmpFile.exists()) {
if (currentapiVersion < 24) {
// 从文件中创建uri
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
} else {
//兼容android7.0 使用共享文件的形式
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, mTmpFile.getAbsolutePath());
Uri uri = getActivity().getApplication().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
startActivityForResult(cameraIntent, REQUEST_CAMERA);
} else {
Toast.makeText(getActivity(), "图片错误", Toast.LENGTH_SHORT).show();
}

    } else {
        Toast.makeText(getActivity(), R.string.msg_no_camera, Toast.LENGTH_SHORT).show();
    }
}

@gdky005
Copy link

gdky005 commented Jan 8, 2018

来一个简单粗暴的解决办法:

http://blog.csdn.net/huangxiaoguo1/article/details/52830015?locationNum=2&fps=1

在Application的onCreat()方法中添加以下代码:

       // android 7.0系统解决拍照的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure(); 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants