简体   繁体   English

相机应用程序在 Marshmallow API 23 中运行良好,但是在 API 中拍摄大于 23 的相机时崩溃

[英]Camera app working fine in Marshmallow API 23, but while taking camera in API grater than 23 is crashing

I created a simple app to taking photos that will store in the SURVEYASSIST folder in the SD card.我创建了一个简单的应用程序来拍摄照片,该应用程序将存储在 SD 卡的SURVEYASSIST文件夹中。 It is working fine in my mobile Redmi 3s prime (Android version Marshmallow 6.0.1), but when I open the app in Redmi Note 5 pro (Oreo 8.1.0) and click on the camera button, the app is crashing.它在我的移动 Redmi 3s prime(Android 版本 Marshmallow 6.0.1)中运行良好,但是当我在 Redmi Note 5 pro(Oreo 8.1.0)中打开应用程序并单击相机按钮时,应用程序崩溃了。 And error code is attaching as seen in the image.如图所示,附加了错误代码。

错误代码截图

public class MainActivity extends AppCompatActivity {

    public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final String Demo_ImagePath ="/storage/emulated/0/SURVEYASSIST/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                String imgcurTime = dateFormat.format(new Date());
                String _path = Demo_ImagePath + File.separator + imgcurTime + ".jpg";
                File file = new File(_path);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
            }
        });
    }
}

The FileUriExposedException is what is being thrown. FileUriExposedException是被抛出的。 From documentation :文档

The exception that is thrown when an application exposes a file:// Uri to another app.当应用程序向另一个应用程序公开 file:// Uri 时引发的异常。

There's a good article on how to fix this here but I will summarize in case the link goes dead at some point: 这里有一篇关于如何解决这个问题的好文章,但我会总结一下,以防链接在某个时候失效:

  1. Add this to your manifest:将此添加到您的清单中:

     <manifest...> <application...> <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> </application>

  2. Create XML file res/xml/provider_paths.xml创建 XML 文件 res/xml/provider_paths.xml

     <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths>
  3. Change your code: From:更改您的代码:来自:

     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

    To:至:

     Uri apkURI = FileProvider.getUriForFile( this, this.getApplicationContext().getPackageName() + ".provider", file); intent.setDataAndType(apkURI, "image/*"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM