Android call system camera

I have been studying the "First Line of Code" by Guo Lin for the past two days.
When I worked hard to call the camera code in Chapter 8, I found that the app crashed when I clicked the button.
I found a lot of information on the Internet, but I couldn't find out what was wrong. Just now, I found a very stupid question. . . . ——No
permission to open the camera ,
hahaha. I don’t know if there are any friends who made the same mistake as me.
Also,
path="" in file_paths.xml will also report an error, modify it to path="," can run successfully

<?xml version='1.0' encoding= "utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="my_images"
        path="path"/>
</paths>

In addition,
when looking for a solution, I also learned a very good method to determine whether the camera permission is turned on. You can refer to it if necessary.

 if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
    
    
            ActivityCompat.requestPermissions(this, new String[]{
    
    Manifest.permission.CAMERA}, 0);}

After adding this sentence, if the app does not have the camera permission, a pop-up box will pop up whether to enable the camera permission.

Insert picture description here
The above are some of the problems and solutions encountered in the learning process recently. If there is something wrong, please give pointers. Let's discuss it together and welcome comments.

Guess you like

Origin blog.csdn.net/s_t_r_u_g_g_l_e_/article/details/108036530