14

1- Some colleague (who is an android developer using android studio) is facing a problem when he tests his application on my phone (which is a nexus 5 with android 6.0.1), the problem he is facing is that when he takes an image from a gallery it works with him ok, but when taking a photo from the camera it displays none, he suspects that the image he is saving on the phone don't get saved, he suspects it's a permission problem, for him it works on galaxy but not on nexus.

2- the very same thing happens with me in unity I bought a plugin that used to work OK in the past on my phone (when I take a camera photo) and now after I updated It's no longer taking the image, I mean I go to camera and go back to application without having anything loaded into the view like before.

this is what logcat shows

--------- beginning of main 
02-07 16:36:10.272      203-813/? D/audio_hw_primary﹕ out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2 
02-07 16:36:10.282      203-813/? D/audio_hw_primary﹕ select_devices: out_snd_device(2: speaker) in_snd_device(0: none) 
02-07 16:36:10.282      203-813/? D/msm8974_platform﹕ platform_send_audio_calibration: sending audio calibration for snd_device(2) acdb_id(15) 
02-07 16:36:10.282      203-813/? D/audio_hw_primary﹕ enable_snd_device: snd_device(2: speaker) 
02-07 16:36:10.284      203-813/? D/audio_hw_primary﹕ enable_audio_route: apply and update mixer path: low-latency-playback 
02-07 16:36:10.289  24484-24484/? W/CAM_ActivityCloser﹕ WARNING: Activity was artificially finished: CameraActivityController was closed. 
02-07 16:36:10.302  24484-24484/? I/CAM_2RsmeActvtyFltr﹕ START onPause: Activity = com.android.camera.util.activity.DoubleOnResumeActivityFilter@d1e256d 
02-07 16:36:10.303  24484-27584/? W/CAM2PORT_AndCamAgntImp﹕ Releasing camera without any camera opened. 
02-07 16:36:10.305  24484-24484/? I/CAM_2RsmeActvtyFltr﹕ END onPause: Activity = com.android.camera.util.activity.DoubleOnResumeActivityFilter@d1e256d 
02-07 16:36:10.316  25275-25275/? D/CameraController﹕ onActivityResult 
02-07 16:36:10.316  25275-25275/? D/CameraController﹕ RESULT_OK 
02-07 16:36:10.329  25275-25275/? I/Unity﹕ **[CameraDemo] onCaptureImageFail**

Why this happens? And how to solve it?

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75
  • this problem is incredibly difficult: http://stackoverflow.com/a/7411824/294884 http://stackoverflow.com/a/22282988/294884 http://stackoverflow.com/a/17922913/294884 – Fattie Feb 07 '16 at 17:54
  • See http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE: *if you app targets M and above and declares as using the CAMERA permission which is not granted, then atempting to use this action will result in a SecurityException.* – Alex Cohn Feb 07 '16 at 21:55

5 Answers5

2

You have to ask for permission in activity

in your function :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.System.canWrite(this)) {
            requestPermissions(new String[]{Manifest.permission.CAMERA,
                    Manifest.permission.CAMERA}, REQUEST_CAMERA);
        } else {
            takeFromCamera();
        }
    } else {
        takeFromCamera();
    }

and add this in your activity

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_CAMERA) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                takeFromCamera();
            } else {
                Log.e("Permission", "Denied");
            }
    }
}

in manifest :

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
MrZ
  • 560
  • 2
  • 12
1

I don't know much about unity and pardon me if this doesn't help. In general, in order for everything to work properly regarding using the camera hardware and storing the image in native android, the androidmanifest.xml file should include the following inside the application tag:

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

To tweak the manifest file from unity, here's a guide I found: https://matthewongamedesign.wordpress.com/2013/06/08/unity-and-the-android-manifest-file/

Ilan Kutsman
  • 469
  • 3
  • 9
1

the solution was to ask for permission in an alert, not only grant the permission in manifest.

DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75
  • Can you clarify what you mean here? I'm getting this error message when I launch the ACTION_IMAGE_CAPTURE intent, which afaict doesn't require any permissions (since your app isn't the one actually talking to the Camera) – Eric Jan 26 '17 at 00:38
0

It does look like an runtime permission check issue. Android has introduced runtime permission check from API 23(marshmallow) onward.

http://developer.android.com/training/permissions/requesting.html

I hope this is helpful.

Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
  • yeah, i wrote the answer already, one hour ago below and accepted it, thanks gaurav4sarma. – DeyaEldeen Feb 21 '16 at 16:14
  • 1
    I am sorry but I thought this would be helpful for all the future visitors because your answer never mentions about runtime permission check hence the link. Anyways never mind i hope someone somewhere finds it helpful, Regards – Gaurav Sarma Feb 21 '16 at 16:47
0

have you provided the permission for accessing the gallery ? Please post your code ..

Vivek Bhardwaj
  • 530
  • 5
  • 16