microsoft / Cognitive-Face-Android

Cognitive Services Face client library for Android.
https://www.microsoft.com/cognitive-services/en-us/face-api
Other
269 stars 151 forks source link

Sample app crashes on Take picture option #49

Open hunny12993 opened 5 years ago

hunny12993 commented 5 years ago

Application is crashing when I select "Take picture" Option.

12-01 11:25:21.648 15151-15151/com.microsoft.projectoxford.faceapisample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.microsoft.projectoxford.faceapisample, PID: 15151 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:6305) at android.view.View$PerformClick.run(View.java:24840) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6501) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:6305)  at android.view.View$PerformClick.run(View.java:24840)  at android.os.Handler.handleCallback(Handler.java:790)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6501)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.microsoft.projectoxford.faceapisample/files/Pictures/IMG_4425260963990859387.jpg exposed beyond app through ClipData.Item.getUri() at android.os.StrictMode.onFileUriExposed(StrictMode.java:1962) at android.net.Uri.checkFileUriExposed(Uri.java:2356) at android.content.ClipData.prepareToLeaveProcess(ClipData.java:942) at android.content.Intent.prepareToLeaveProcess(Intent.java:9850) at android.content.Intent.prepareToLeaveProcess(Intent.java:9835) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1612) at android.app.Activity.startActivityForResult(Activity.java:4514) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767) at android.app.Activity.startActivityForResult(Activity.java:4472) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754) at com.microsoft.projectoxford.face.samples.ui.SelectImageActivity.takePhoto(SelectImageActivity.java:115) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)  at android.view.View.performClick(View.java:6305)  at android.view.View$PerformClick.run(View.java:24840)  at android.os.Handler.handleCallback(Handler.java:790)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6501)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

LuolinRowling commented 5 years ago

This may because that if targetSdkVersion is >= Android 6.0(API level 23), developer need to add some code for request related permissions from the user at the runtime.

If you still want to implement the "Take Picture" function, you should request the permisson of CAMERA for take pictures and storage related permisson for saving pictures at the runtime. And maybe FileProvider can be used to store pictures.

You can refer to https://developer.android.com/training/permissions/requesting and https://developer.android.com/reference/android/support/v4/content/FileProvider

Hope this could help you.

lebronJ commented 5 years ago

Thanks Lin for the PR #54 to fix this issue. @hunny12993 please have a try with it and let me know if you still face the problem by any chance.

Mirofq commented 3 years ago

This may because that if targetSdkVersion is >= Android 6.0(API level 23), developer need to add some code for request related permissions from the user at the runtime.

If you still want to implement the "Take Picture" function, you should request the permisson of CAMERA for take pictures and storage related permisson for saving pictures at the runtime. And maybe FileProvider can be used to store pictures.

You can refer to https://developer.android.com/training/permissions/requesting and https://developer.android.com/reference/android/support/v4/content/FileProvider

Hope this could help you.

I tried implement the tutorial but still it doesnt work. it gives me a lot of error and take photo function still crashes. Is there a way I could use sdk 28 for this function? Im compiling this project with other library so as for now only target sdk28 allow my whole app to run fine. except this take photo function crashes.

Mirofq commented 3 years ago

Thanks Lin for the PR #54 to fix this issue. @hunny12993 please have a try with it and let me know if you still face the problem by any chance.

I'm having a big problem on this matter although I've tried the tutorial. I need to use this function at target sdk 28. Developer please add some code for request related permissions from the user at the runtime. Thanks a lot.