Camera is not working in Nougat 7.0

后端 未结 4 1622
感动是毒
感动是毒 2021-02-15 12:25

My camera code is working in all Android versions but in Nougat 7.0 it gives the following error:

java.lang.NullPointerException: Attempt to invoke virtual meth         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-15 12:36

    Try this its not the intent that create the problem once you take the picture and save to the SD card and getting back the URI is different in Nougat....

    It is quite easy to implement FileProvider on your application. First you need to add a FileProvider tag in AndroidManifest.xml under tag like below: AndroidManifest.xml

    
    
                
            
        
    
    

    And then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist.

    res/xml/provider_paths.xml

     
        
            
        
    

    Done! FileProvider is now declared and be ready to use.

    The final step is to change the line of code below in MainActivity.java

     Uri photoURI = Uri.fromFile(createImageFile());
    

    to

    Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
            BuildConfig.APPLICATION_ID + ".provider",
            createImageFile());
    

    And .... done ! Your application should now work perfectly fine on any Android version including Android Nougat. Cheers !

提交回复
热议问题