Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
jeff_sawatzky
Posts: 29
Registered: ‎04-01-2013
My Device: Z10
My Carrier: Rogers
Accepted Solution

Cordova 3.2.0 Capture Plugin not working

[ Edited ]

I have a cordova 3.2.0 app that uses the Capture plugin. When I launch the image capture, I can take a photo, the response comes back with a fileURI (eg "file:///accounts/1000/shared/camera/IMG_00000004.jpg"), but I can't do anything with it.

 

The cordova documentation says that it adds elements to the config.xml:

http://cordova.apache.org/docs/en/3.1.0/cordova_media_capture_capture.md.html#Capture

 

Specifically it is supposed to do:

(in www/plugins.xml)
<feature name="Capture">
   
<param name="blackberry-package" value="org.apache.cordova.capture.MediaCapture"/>
</feature>


(in www/
config.xml)
<feature id="blackberry.system"  required="true" version="1.0.0.0"/>
<feature id="blackberry.io.file" required="true" version="1.0.0.0"/>

 

However, when I extract the config.xml from the generated bar, none of those elements are there and the plugins.xml doesn't exist. Are they not needed anymore? Are the plugins incorrect?

 

I should also mention that I tried manually adding these properties to the config.xml, but that doesn't work as the file seems to be autogenerated at each build.

 

Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Cordova 3.2.0 Capture Plugin not working

Hi there, if the API is returning the file path, it would seem that the API is working correctly. What is the issue you're actually seeing when you say "but I can't do anything with it." Have you connected Web Inspector / do any issues show up there?

To confirm that the file exists, you could try setting the *src* of an <image> element to the full file:/// path.

If you get an access denied error, you could try whitelisting the "file:///" URI in your config.xml with an <access> element.

Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
Developer
jeff_sawatzky
Posts: 29
Registered: ‎04-01-2013
My Device: Z10
My Carrier: Rogers

Re: Cordova 3.2.0 Capture Plugin not working

The <image> thing is exactly what I am trying to do, but it fails to load. I will try whitelisting the "file:///" URI in my config.xml with an <access> element and see if that does anything.
Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Cordova 3.2.0 Capture Plugin not working

Sounds good. Let us know if it doesn't work (it likely will be an <access> issue and we just need to find the right one) and I'll try to drum up a working sample here.

Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
Developer
jeff_sawatzky
Posts: 29
Registered: ‎04-01-2013
My Device: Z10
My Carrier: Rogers

Re: Cordova 3.2.0 Capture Plugin not working

According to the docs:

http://cordova.apache.org/docs/en/3.1.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide

 

Access to all domains, including file:// protocol:

<access origin="*" subdomains="true"/>

 

I added that, and no dice. I also tried:

<access origin="file://*" subdomains="true"/>

 

That didn't help either.

Please use plain text.
Developer
jeff_sawatzky
Posts: 29
Registered: ‎04-01-2013
My Device: Z10
My Carrier: Rogers

Re: Cordova 3.2.0 Capture Plugin not working

On top of not being able to add the path to an image tag, I also try to do a resolveLocalFileSystemURI on the returned fileURI (eg "file:///accounts/1000/shared/camera/IMG_00000007.jpg") and it returns with error code 5 (which I think is FileError.ENCODING_ERR)
Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Cordova 3.2.0 Capture Plugin not working

Hi Jeff, I just managed to get a sample of this working. Here is what I did.

 

From the command-line, here are the webworks commands I ran:

 

C:\webworks>webworks create capture

C:\webworks>cd capture

C:\webworks\capture>webworks plugin add org.apache.cordova.media-capture

C:\webworks\capture>webworks run

 

Prior to executing the webworks run command, I madea few modifications. To config.xml, at the root www folder I added:

 

	<access origin="file:///" subdomains="true" />
	<rim:permissions>
		<rim:permit>access_shared</rim:permit>
	</rim:permissions>

 

Note that the <access> origin has three / characters.

 

In my config.xml, I added two HTML element for my testing, wrapped in a div:

 

		<div>
			<img id="imgTarget" width="480" height="480" src="" />
			<button onclick="captureImage();">Capture Image</button> <br>
		</div>

 And I defined the captureImage function as follows:

 

        <script type="text/javascript">
			function captureImage() {
				navigator.device.capture.captureImage(
					function captureSuccess(result) {
						document.querySelector('#imgTarget').src='file://' + result[0].fullPath;
					},
					function captureError(result) {
						console.log('error');
						console.log(result);
					}
				);
			}
		
            app.initialize();
        </script>

 

Note that the app.initialize() call is from the auto-generated code. I left it in just to show where in the code I added the function.

 

With this, the <img> element successfully loads the image.

 


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
Developer
jeff_sawatzky
Posts: 29
Registered: ‎04-01-2013
My Device: Z10
My Carrier: Rogers

Re: Cordova 3.2.0 Capture Plugin not working

So, this got me a little closer, though it still isn't perfect.

 

First of all, I am using cordova, not webworks, so some of the workflow is different. For instance, I had to add the following to my cordova-app/www/config.xml

 

<rim:permissions>
	<rim:permit>access_shared</rim:permit>
</rim:permissions>

 

It appears that the following was not needed:

 

<access origin="file:///" subdomains="true" />

 

If I tried adding it to my cordova-app/platforms/blackberry10/config.xml, it wouldn't work as each time I do a build that file is recreated from the common one. Having to add that to the common one feels wrong. Also, the build seems to automatically add the following:

 

<rim:permissions>
	<rim:permit>read_device_identifying_information</rim:permit>
</rim:permissions>

 

So why doesn't it add the permission for the access_shared (which seems required by the capture plugin...no use taking a photo if the app can't access it, amiright)?

 

So anyway, I can now add the fileURI to an <image> tag to see it. But I still get the error code 5 when I try to resolveLocalFileSystemURI in order to get the FileEntry so that I can get the filePath to send to a FileTransfer.upload.

 

 

Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Cordova 3.2.0 Capture Plugin not working

Regarding the automatic inclusion of the permission, you are correct. I can't speak for the Cordova SDK, but our new WebWorks SDK (which is Cordova-based as well) aims to add all dependencies when a plugin is added. I've escalated this issue to our development teams to be addressed in the next release.

For the FileTransfer path, have you tried dropping the file:// portion of the URL? I recall having issues with similar APIs here:
https://github.com/blackberry/BB10-WebWorks-Framework/issues/536#issuecomment-12074640

You can find my initial setup at the top of that post. I realize the error (5) is different than the error in the issue report, but it could serve as a basis of comparison.

One other thing to confirm is that you've white-listed the URL you are trying to upload to with an <access> element. If you can confirm the upload path and config.xml <access> declaration, that would be ideal. Feel free to modify the words in the URL to hide the actual URL if you like, but please leave the structure intact.

Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
Developer
jeff_sawatzky
Posts: 29
Registered: ‎04-01-2013
My Device: Z10
My Carrier: Rogers

Re: Cordova 3.2.0 Capture Plugin not working

Is dropping "file://" safe? Couldn't the fileURI point to a virtual path which gets mapped to a completely different physical path once resolved? Isn't this why the resolveLocalFileSystemURI function exists? Also, on Android I get a content:// uri, which needs to get resolved to a physical path before I do the file transfer. Keep in mind that I am using cordova because I am targetting multiple platforms, and I don't really want to have to write special code just for BlackBerry (cordova is supposed to do this for us). Another bonus to resolveLocalFileSystemURI  is that it gives me a FileEntry that makes it super easy to get the file name, instead of having to parse the file name out of the uri manually.

 

But I digress. Getting back to the issue...

 

I've stepped into window.resolveLocalFileSystemURI, and it goes to:

org.apache.cordova.file/www/blackberry10/resolveLocalFileSystemURI.js

 

I put a breakpoint on line 40 where it does:

if(fileUtils.isOutsideSandbox(stripURI(decodedURI))){
    cordova.exec(null,null,"org.apache.cordova.file","setSandbox",[false]);
}else{
    cordova.exec(null,null,"org.apache.cordova.file","setSandbox",[true]);
}

 

 

at this point (before it has executed fileUtils.isOutsideSandbox):

sandboxState = true

decodedURI = "file:///accounts/1000/shared/camera/IMG_00000012.jpg"

 

and the result of fileUtils.isOutsideSandbox seems to be false (which suggests that the file is inside the sandbox, in other words local), and therefore goes into the else and sets sandbox state to true.

 

it then does:

window.webkitResolveLocalFileSystemURL(decodedURI,function(entry){
    success(fileUtils.createEntry(entry));
},function(e){
    window.webkitResolveLocalFileSystemURL(decodedURI+'/',function(entry){
        success(fileUtils.createEntry(entry));
    },function(e){
        fail(e);
    });
});
 
and both webkitResolveLocalFileSystemURL calls return error code 5  which is FileError.ENCODING_ERR.
 
So, to me, it seems like a bug in the webkitResolveLocalFileSystemURL implementation as I am passing a valid local file url but it is returning an error.
 
As for whether or not the upload path is whitelisted, it is our REST api, and the app is successfully GETting things from it, as well as POSTing other things to it, so it is whitelisted. As I mentioned previously, I am running into an issue with resolving the fileURI to a filePath, I haven't even got to the FileTransfer part (which needs the filePath).
Please use plain text.