Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot take pictures on Android #416

Closed
Owlbertz opened this issue Jan 28, 2019 · 5 comments · Fixed by #510
Closed

Cannot take pictures on Android #416

Owlbertz opened this issue Jan 28, 2019 · 5 comments · Fixed by #510

Comments

@Owlbertz
Copy link

Hi,

I am using the camera plugin to take pictures and to select photos from the gallery. While the latter works perfectly fine, the former action fails with this error:

length=1; index=1

I was able to find out the source, but not the cause:

"F09 Camera1798421768 sjava.lang.ArrayIndexOutOfBoundsException: length=1; index=1
	 at org.apache.cordova.camera.CordovaUri.getFileNameFromUri(CordovaUri.java:98)
	 at org.apache.cordova.camera.CordovaUri.<init>(CordovaUri.java:52)
	 at org.apache.cordova.camera.CameraLauncher.takePicture(CameraLauncher.java:330)
	 at org.apache.cordova.camera.CameraLauncher.callTakePicture(CameraLauncher.java:308)
	 at org.apache.cordova.camera.CameraLauncher.execute(CameraLauncher.java:184)
	 at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
	 at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
	 at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
	 at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
	 at android.os.MessageQueue.nativePollOnce(Native Method)
	 at android.os.MessageQueue.next(MessageQueue.java:323)
	 at android.os.Looper.loop(Looper.java:136)
	 at android.os.HandlerThread.run(HandlerThread.java:61)

Arguments provided were to the Java-file were:
0: 70
1: 1
2: 1
3: 1024
4: -1
5: 0
6: 0
7: false
8: true
9: false
10: null
11: 0

I hope someone has an idea what I am doing wrong.

@hazems
Copy link
Member

hazems commented Feb 24, 2019

How do you launch camera, can you share your code? What is your Android device/OS version information?

@pvillaverde
Copy link

pvillaverde commented Feb 27, 2019

Hey there!

I'm having the same issue using Ionic. It works when using library but if I try to use the camera it gives the same error.
Here's the code I used:

function takePicture(camera) {
	const options = {
		//	quality: 90, //Number	Quality of the saved image, range of 0 - 100
		destinationType: $cordovaCamera.DestinationType.FILE_URI, //Number	Format of the return value
		sourceType: camera ? $cordovaCamera.PictureSourceType.CAMERA : $cordovaCamera.PictureSourceType.PHOTOLIBRARY, //Number	Set the source of the picture
		allowEdit: false, //Boolean	Allow simple editing of image before selection
		encodingType: $cordovaCamera.EncodingType.JPEG, //Number	JPEG = 0, PNG = 1
		//	targetWidth: 300, //Number	Width to scale image (pixels). Used with targetHeight
		//	targetHeight: 300, //Number	Height to scale image (pixels). Used with targetHeight
		mediaType: $cordovaCamera.MediaType.PICTURE, //String	Set the type of media to select from
		cameraDirection: 0, //Number	Back = 0, Front-facing = 1
		//	popoverOptions: "",	//String	iOS-only options that specify popover location in iPad
		saveToPhotoAlbum: true, //Boolean	Save image to photo album on the device
		correctOrientation: true, //Boolean	correct camera captured images in case wrong orientation
	};
	$cordovaCamera.getPicture(options).then(console.log).catch(console.error);
}

Using Ionic v1 and Cordova:

  • Ionic Framework : ionic1 1.3.4
  • cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
  • Cordova Platforms : android 7.1.4
  • cordova-plugin-camera 4.0.3 "Camera"
  • Tested on device: Motorola G4Play with Android 7.1.1

Also as @Owlbertz , args passed to the Java-file:

0: 50
1: 1
2: 1
3: -1
4: -1
5: 0
6: 0
7: false
8: true
9: true
10: null
11: 0

@pvillaverde
Copy link

pvillaverde commented Feb 28, 2019

So far I've located the problem on CordovaUri.getFileNameFromUri
When doing

        String partial_path = fullUri.split("external_files")[1];

The array does not split in two because the fullUri I have is content://com.application.id.provider/external_cache/.Pic.jpgp having external_cache instead of external_files. So that's what's causing the IndexOutOfBounds Exception.

In my case I've found this is due to other plugin, "cordova-open", which I use to open downloaded files.
Cordova camara establish an "external-path" in camera_provider_paths.xml

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

while cordova-open establishes another in provider_paths.xml.

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

I'm not really into the android environment, but I guess some conflict exists there. And for me to work I had to remove external-files-path & external-cache-path and specify external-path name with "external_files", same as in cordova-camera. Just naming them the same way caused another error afterwards on which the file were not found.

If someone can point me into a better solution it would be appreciated. Maybe there should be something that checks the storage location, but I read something on the CordovaUri regarding there was no method to do it.

@hudawei996
Copy link

@gonself
Copy link

gonself commented Oct 1, 2020

hi!
i have the same issue with an ionic app...
@Huyongqiang after make this changes, what do you do? need to recompile or something? with ionic cordova android build still not works...

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants