Cordova-plugin-camera not working with Android version 8









up vote
2
down vote

favorite
1












I am using cordova-plugin-camera to access the camera in Android. This was working fine until I changed the Target SDK version to API 26: Android 8.0 (Oreo) in order to be able to upload to the Google play store which now requires this version or above.



The error I get is




file:///data/user/0/mypackage/cache/Capture.jpg exposed beyond app through ClipData.Item.getUri()




which I believe is due to a change in how apps can access the file system from Android Nougat - see android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData().



I have tried fixing the issue with different versions of cordova, cordova android and the camera plugin as per https://github.com/ionic-team/ng-cordova/issues/1381 but in each case as soon as I change the TargetSDK to 26 I get the same error on an Android 8 phone.



I have added the following to my AndroidManifest.xml file



 <provider android:authorities="com.okappy.okappy.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
</provider>


And I have checked that I have a camera_provider_paths.xml with the following



 <paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>




I have also tried editing the following lines in cordova/camera/CordovaURI.java to forcibly ignore the fileUri but it made no difference



 public Uri getCorrectUri()

/*if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return androidUri;
else
return fileUri;
*/
return androidUri;




I have the following versions



  • 8.1.2 (cordova-lib@8.1.1)


  • android 7.1.2


  • cordova-plugin-camera 4.0.3


  • cordova-plugin-console 1.0.3

  • cordova-plugin-file 6.0.1

I am using Compile SDK version 28.0.3, MinumumSDK version of 21 and TargetSDK version of 26.



Thanks in advance for your help










share|improve this question

























    up vote
    2
    down vote

    favorite
    1












    I am using cordova-plugin-camera to access the camera in Android. This was working fine until I changed the Target SDK version to API 26: Android 8.0 (Oreo) in order to be able to upload to the Google play store which now requires this version or above.



    The error I get is




    file:///data/user/0/mypackage/cache/Capture.jpg exposed beyond app through ClipData.Item.getUri()




    which I believe is due to a change in how apps can access the file system from Android Nougat - see android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData().



    I have tried fixing the issue with different versions of cordova, cordova android and the camera plugin as per https://github.com/ionic-team/ng-cordova/issues/1381 but in each case as soon as I change the TargetSDK to 26 I get the same error on an Android 8 phone.



    I have added the following to my AndroidManifest.xml file



     <provider android:authorities="com.okappy.okappy.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
    </provider>


    And I have checked that I have a camera_provider_paths.xml with the following



     <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>




    I have also tried editing the following lines in cordova/camera/CordovaURI.java to forcibly ignore the fileUri but it made no difference



     public Uri getCorrectUri()

    /*if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    return androidUri;
    else
    return fileUri;
    */
    return androidUri;




    I have the following versions



    • 8.1.2 (cordova-lib@8.1.1)


    • android 7.1.2


    • cordova-plugin-camera 4.0.3


    • cordova-plugin-console 1.0.3

    • cordova-plugin-file 6.0.1

    I am using Compile SDK version 28.0.3, MinumumSDK version of 21 and TargetSDK version of 26.



    Thanks in advance for your help










    share|improve this question























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I am using cordova-plugin-camera to access the camera in Android. This was working fine until I changed the Target SDK version to API 26: Android 8.0 (Oreo) in order to be able to upload to the Google play store which now requires this version or above.



      The error I get is




      file:///data/user/0/mypackage/cache/Capture.jpg exposed beyond app through ClipData.Item.getUri()




      which I believe is due to a change in how apps can access the file system from Android Nougat - see android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData().



      I have tried fixing the issue with different versions of cordova, cordova android and the camera plugin as per https://github.com/ionic-team/ng-cordova/issues/1381 but in each case as soon as I change the TargetSDK to 26 I get the same error on an Android 8 phone.



      I have added the following to my AndroidManifest.xml file



       <provider android:authorities="com.okappy.okappy.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
      </provider>


      And I have checked that I have a camera_provider_paths.xml with the following



       <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-path name="external_files" path="."/>




      I have also tried editing the following lines in cordova/camera/CordovaURI.java to forcibly ignore the fileUri but it made no difference



       public Uri getCorrectUri()

      /*if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
      return androidUri;
      else
      return fileUri;
      */
      return androidUri;




      I have the following versions



      • 8.1.2 (cordova-lib@8.1.1)


      • android 7.1.2


      • cordova-plugin-camera 4.0.3


      • cordova-plugin-console 1.0.3

      • cordova-plugin-file 6.0.1

      I am using Compile SDK version 28.0.3, MinumumSDK version of 21 and TargetSDK version of 26.



      Thanks in advance for your help










      share|improve this question













      I am using cordova-plugin-camera to access the camera in Android. This was working fine until I changed the Target SDK version to API 26: Android 8.0 (Oreo) in order to be able to upload to the Google play store which now requires this version or above.



      The error I get is




      file:///data/user/0/mypackage/cache/Capture.jpg exposed beyond app through ClipData.Item.getUri()




      which I believe is due to a change in how apps can access the file system from Android Nougat - see android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData().



      I have tried fixing the issue with different versions of cordova, cordova android and the camera plugin as per https://github.com/ionic-team/ng-cordova/issues/1381 but in each case as soon as I change the TargetSDK to 26 I get the same error on an Android 8 phone.



      I have added the following to my AndroidManifest.xml file



       <provider android:authorities="com.okappy.okappy.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
      </provider>


      And I have checked that I have a camera_provider_paths.xml with the following



       <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-path name="external_files" path="."/>




      I have also tried editing the following lines in cordova/camera/CordovaURI.java to forcibly ignore the fileUri but it made no difference



       public Uri getCorrectUri()

      /*if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
      return androidUri;
      else
      return fileUri;
      */
      return androidUri;




      I have the following versions



      • 8.1.2 (cordova-lib@8.1.1)


      • android 7.1.2


      • cordova-plugin-camera 4.0.3


      • cordova-plugin-console 1.0.3

      • cordova-plugin-file 6.0.1

      I am using Compile SDK version 28.0.3, MinumumSDK version of 21 and TargetSDK version of 26.



      Thanks in advance for your help







      android cordova cordova-plugins






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 10:36









      Richard

      136




      136






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          The issue is with our code rather than the plugin - view issue on github



          We were using the following code



          function captureImage() 
          navigator.device.capture.captureImage(captureImageSuccess, captureImageError, limit: 1);


          function captureImageSuccess(mediaFiles)
          uploadFile(mediaFiles[0]);


          function uploadFile(mediaFile,fileType) //#F333
          var win = function (r)


          var options = new FileUploadOptions();
          ImageURL=mediaFile.fullPath;
          options.fileName = (fileType)? mediaFile.name : mediaFile.file; //#F333
          options.mimeType = "multipart/form-data";
          var params = new Object();
          params.fileName = 'AndroidPhoneUpload';
          params.imageType = (fileType)?fileType:'Picture'; //#F333
          var printName = prompt("Add a title",""); //#F644
          if (printName)
          params.printedName = printName;

          window.resolveLocalFileSystemURL(ImageURL, function(fileEntry)
          fileEntry.file(function(fileObj)
          params.fileSize = ""+fileObj.size;
          );
          );
          options.params = params;
          var ft = new FileTransfer();
          ft.upload(ImageURL, encodeURI("uploadURL"),win,fail,options);



          Which we have replaced with



          navigator.camera.getPicture(function cameraSuccess(imageUri) 
          var options = new FileUploadOptions();
          options.fileKey="file";
          options.fileName="test";
          options.mimeType="multipart/form-data";
          var params = new Object();
          params.fileName = 'AndroidPhoneUpload';
          options.params = params;
          var ft = new FileTransfer();
          ft.upload(imageUri, encodeURI("https://upload.okappy.com/UploadToS3"), win, fail, options);
          , function captureImageError(error)
          console.debug("Unable to obtain picture: " + error, "app");
          , options);


          Hope that helps!






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224086%2fcordova-plugin-camera-not-working-with-android-version-8%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            The issue is with our code rather than the plugin - view issue on github



            We were using the following code



            function captureImage() 
            navigator.device.capture.captureImage(captureImageSuccess, captureImageError, limit: 1);


            function captureImageSuccess(mediaFiles)
            uploadFile(mediaFiles[0]);


            function uploadFile(mediaFile,fileType) //#F333
            var win = function (r)


            var options = new FileUploadOptions();
            ImageURL=mediaFile.fullPath;
            options.fileName = (fileType)? mediaFile.name : mediaFile.file; //#F333
            options.mimeType = "multipart/form-data";
            var params = new Object();
            params.fileName = 'AndroidPhoneUpload';
            params.imageType = (fileType)?fileType:'Picture'; //#F333
            var printName = prompt("Add a title",""); //#F644
            if (printName)
            params.printedName = printName;

            window.resolveLocalFileSystemURL(ImageURL, function(fileEntry)
            fileEntry.file(function(fileObj)
            params.fileSize = ""+fileObj.size;
            );
            );
            options.params = params;
            var ft = new FileTransfer();
            ft.upload(ImageURL, encodeURI("uploadURL"),win,fail,options);



            Which we have replaced with



            navigator.camera.getPicture(function cameraSuccess(imageUri) 
            var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName="test";
            options.mimeType="multipart/form-data";
            var params = new Object();
            params.fileName = 'AndroidPhoneUpload';
            options.params = params;
            var ft = new FileTransfer();
            ft.upload(imageUri, encodeURI("https://upload.okappy.com/UploadToS3"), win, fail, options);
            , function captureImageError(error)
            console.debug("Unable to obtain picture: " + error, "app");
            , options);


            Hope that helps!






            share|improve this answer
























              up vote
              0
              down vote













              The issue is with our code rather than the plugin - view issue on github



              We were using the following code



              function captureImage() 
              navigator.device.capture.captureImage(captureImageSuccess, captureImageError, limit: 1);


              function captureImageSuccess(mediaFiles)
              uploadFile(mediaFiles[0]);


              function uploadFile(mediaFile,fileType) //#F333
              var win = function (r)


              var options = new FileUploadOptions();
              ImageURL=mediaFile.fullPath;
              options.fileName = (fileType)? mediaFile.name : mediaFile.file; //#F333
              options.mimeType = "multipart/form-data";
              var params = new Object();
              params.fileName = 'AndroidPhoneUpload';
              params.imageType = (fileType)?fileType:'Picture'; //#F333
              var printName = prompt("Add a title",""); //#F644
              if (printName)
              params.printedName = printName;

              window.resolveLocalFileSystemURL(ImageURL, function(fileEntry)
              fileEntry.file(function(fileObj)
              params.fileSize = ""+fileObj.size;
              );
              );
              options.params = params;
              var ft = new FileTransfer();
              ft.upload(ImageURL, encodeURI("uploadURL"),win,fail,options);



              Which we have replaced with



              navigator.camera.getPicture(function cameraSuccess(imageUri) 
              var options = new FileUploadOptions();
              options.fileKey="file";
              options.fileName="test";
              options.mimeType="multipart/form-data";
              var params = new Object();
              params.fileName = 'AndroidPhoneUpload';
              options.params = params;
              var ft = new FileTransfer();
              ft.upload(imageUri, encodeURI("https://upload.okappy.com/UploadToS3"), win, fail, options);
              , function captureImageError(error)
              console.debug("Unable to obtain picture: " + error, "app");
              , options);


              Hope that helps!






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                The issue is with our code rather than the plugin - view issue on github



                We were using the following code



                function captureImage() 
                navigator.device.capture.captureImage(captureImageSuccess, captureImageError, limit: 1);


                function captureImageSuccess(mediaFiles)
                uploadFile(mediaFiles[0]);


                function uploadFile(mediaFile,fileType) //#F333
                var win = function (r)


                var options = new FileUploadOptions();
                ImageURL=mediaFile.fullPath;
                options.fileName = (fileType)? mediaFile.name : mediaFile.file; //#F333
                options.mimeType = "multipart/form-data";
                var params = new Object();
                params.fileName = 'AndroidPhoneUpload';
                params.imageType = (fileType)?fileType:'Picture'; //#F333
                var printName = prompt("Add a title",""); //#F644
                if (printName)
                params.printedName = printName;

                window.resolveLocalFileSystemURL(ImageURL, function(fileEntry)
                fileEntry.file(function(fileObj)
                params.fileSize = ""+fileObj.size;
                );
                );
                options.params = params;
                var ft = new FileTransfer();
                ft.upload(ImageURL, encodeURI("uploadURL"),win,fail,options);



                Which we have replaced with



                navigator.camera.getPicture(function cameraSuccess(imageUri) 
                var options = new FileUploadOptions();
                options.fileKey="file";
                options.fileName="test";
                options.mimeType="multipart/form-data";
                var params = new Object();
                params.fileName = 'AndroidPhoneUpload';
                options.params = params;
                var ft = new FileTransfer();
                ft.upload(imageUri, encodeURI("https://upload.okappy.com/UploadToS3"), win, fail, options);
                , function captureImageError(error)
                console.debug("Unable to obtain picture: " + error, "app");
                , options);


                Hope that helps!






                share|improve this answer












                The issue is with our code rather than the plugin - view issue on github



                We were using the following code



                function captureImage() 
                navigator.device.capture.captureImage(captureImageSuccess, captureImageError, limit: 1);


                function captureImageSuccess(mediaFiles)
                uploadFile(mediaFiles[0]);


                function uploadFile(mediaFile,fileType) //#F333
                var win = function (r)


                var options = new FileUploadOptions();
                ImageURL=mediaFile.fullPath;
                options.fileName = (fileType)? mediaFile.name : mediaFile.file; //#F333
                options.mimeType = "multipart/form-data";
                var params = new Object();
                params.fileName = 'AndroidPhoneUpload';
                params.imageType = (fileType)?fileType:'Picture'; //#F333
                var printName = prompt("Add a title",""); //#F644
                if (printName)
                params.printedName = printName;

                window.resolveLocalFileSystemURL(ImageURL, function(fileEntry)
                fileEntry.file(function(fileObj)
                params.fileSize = ""+fileObj.size;
                );
                );
                options.params = params;
                var ft = new FileTransfer();
                ft.upload(ImageURL, encodeURI("uploadURL"),win,fail,options);



                Which we have replaced with



                navigator.camera.getPicture(function cameraSuccess(imageUri) 
                var options = new FileUploadOptions();
                options.fileKey="file";
                options.fileName="test";
                options.mimeType="multipart/form-data";
                var params = new Object();
                params.fileName = 'AndroidPhoneUpload';
                options.params = params;
                var ft = new FileTransfer();
                ft.upload(imageUri, encodeURI("https://upload.okappy.com/UploadToS3"), win, fail, options);
                , function captureImageError(error)
                console.debug("Unable to obtain picture: " + error, "app");
                , options);


                Hope that helps!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 4 at 13:58









                Richard

                136




                136



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224086%2fcordova-plugin-camera-not-working-with-android-version-8%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Edmonton

                    Crossroads (UK TV series)