Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera is not working in Nougat 7.0

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 method 'java.lang.String android.net.Uri.toString()' on a null object reference

It is working perfectly on all other versions of android except on android 7.0. I have given run time permission for the camera & the gallery but the camera is still not working. Here is the relevant code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        if(requestCode == Constants.CROPPED_PIC_REQUEST_CODE){
            CropImage.ActivityResult result = (CropImage.ActivityResult) data.getExtras().get(CropImage.CROP_IMAGE_EXTRA_RESULT);
            Uri selectedImageUri = result == null ? null : result.getUri();
            Bitmap bitmap = null;
            Log.d("SetUpProfile","Uri cropped is "+outputFileUri);
            bitmap = getBitmap(selectedImageUri);
    //                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
            circleImageView.setImageBitmap(bitmap);
            finalBitmap = bitmap;
        }
        else if (requestCode == Constants.YOUR_SELECT_PICTURE_REQUEST_CODE) {
            final boolean isCamera;
            if (data == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            Uri selectedImageUri;
            if (isCamera) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                String value = prefs.getString("path", "error");
                selectedImageUri = Uri.parse(value);
            } else {
                selectedImageUri = data == null ? null : data.getData();
            }

            Intent i = new Intent(Five.this,CropImageActivity.class);
            i.putExtra("ImageURI", selectedImageUri.toString());
            startActivityForResult(i,Constants.CROPPED_PIC_REQUEST_CODE);

        }
    }
}

Here is my logcat:-

FATAL EXCEPTION: main
Process: com.sancsvision.wayndr, PID: 31570
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=11, result=-1, data=Intent {  }} to activity {com.sancsvision.wayndr/com.sancsvision.wayndr.Five}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference                     
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)              
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)        
    at android.app.ActivityThread.-wrap20(ActivityThread.java)                      
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)         
    at android.os.Handler.dispatchMessage(Handler.java:102)                     
    at android.os.Looper.loop(Looper.java:154)                      
    at android.app.ActivityThread.main(ActivityThread.java:6077)                        
    at java.lang.reflect.Method.invoke(Native Method)                       
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)      
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                                                     
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
    at com.sancsvision.wayndr.Five.onActivityResult(Five.java:259)
    at android.app.Activity.dispatchActivityResult(Activity.java:6917)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 
    at android.app.ActivityThread.-wrap20(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
like image 640
Anand Jain Avatar asked Jan 31 '17 13:01

Anand Jain


People also ask

Why is my camera app not working?

Step 1: Long-tap on the Camera app icon and open the app info menu. Step 2: Go to Storage & cache menu. Step 3: Tap on Clear cache and you are all set to use a working Camera app on Android.


2 Answers

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <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>
</manifest>

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

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

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 !

like image 106
Jaydip Bhensdadiya Avatar answered Oct 28 '22 09:10

Jaydip Bhensdadiya


This might be related to the change in Nougat which doesn't allow "file://" scheme to be attached to an intent.

Check out this blogpost for more details

https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en

like image 24
Themasterhimself Avatar answered Oct 28 '22 09:10

Themasterhimself