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
DueNorth
Posts: 135
Registered: ‎09-25-2012
My Device: Dev Alpha
My Carrier: Rogers

Checking if a media file exists using WebWorks (BB10)

I am having hard time trying to figure out a best way to check if a media files exists within an app directory (image1.png, image2.png, etc.).

 

I was checking up on using HTML5 FileReader but it seems to require an <INPUT> to select the file. What if you want to loop through an array to check if those files exists within the directory? What is the recommended way? 

 

I checked blackberry.io but it seems to be for SD cards and I am not certain that is the way I should head towards. Any ideas?

 

Thanks.

Please use plain text.
Developer
Nitish_Pheonix
Posts: 29
Registered: ‎09-17-2012
My Device: 9810, 9800, 9700, 9900, 9300
My Carrier: BSNL

Re: Checking if a media file exists using WebWorks (BB10)

Hi DueNorth,
I think this will help you.

ImageSDPath = "file:///SDCard/" + imageName;

// Here you have to mount your sdcard path with your media file name.

if(blackberry.io.file.exists(ImageSDPath)){
alert("File exist.");
}

 

 

Check out this link Check whether or not a given file exists.

 

Thanks & Regards,
Himansu

Thanks & Regards,
Nitish_Pheonix

Hit like if the above post is helpful to you.
Please Accept this as Solution if the above post is a solution for you.
Please use plain text.
Developer
DueNorth
Posts: 135
Registered: ‎09-25-2012
My Device: Dev Alpha
My Carrier: Rogers

Re: Checking if a media file exists using WebWorks (BB10)

Thanks I will give it a shot. The documentation link that you gave doesn't say if this function is even properly supported with BB10. I will give an update if anyone wants to know if it works.

Please use plain text.
Developer
DueNorth
Posts: 135
Registered: ‎09-25-2012
My Device: Dev Alpha
My Carrier: Rogers

Re: Checking if a media file exists using WebWorks (BB10)

I decided on testing both the blackberry.io.file & blackberry.io.dir objects with BB10.

 

 

<feature id="blackberry.io.file" />
<feature id="blackberry.io.dir" />

 Both didn't work. With:

var dirs = blackberry.io.dir.appDirs;
document.write(dirs);

 I get an error: Cannot read property 'dir' of undefined

 

With:

if (blackberry.io.file.exists(filepath)) {
  document.write('...found');
}

 I get an error: Cannot read property 'file' of undefined

 

So both blackberry.io.file and blackberry.io.dir does not work with BB10.

Please use plain text.
Developer
DueNorth
Posts: 135
Registered: ‎09-25-2012
My Device: Dev Alpha
My Carrier: Rogers

Re: Checking if a media file exists using WebWorks (BB10)

I have also tried using Invoke.

https://developer.blackberry.com/html5/api/blackberry.invoke.html

 

function viewPicture() {
    // view picture
    blackberry.invoke.invoke({
        uri: <path to jpg file>,
        action: bb.action.VIEW
    }, onInvokeSuccess, onInvokeError);
}

 However it seems to trigger BOTH Success and Error functions.  Ack...

 

 

Please use plain text.
BlackBerry Development Advisor
twindsor
Posts: 820
Registered: ‎07-15-2008
My Device: Z10
My Carrier: Bell

Re: Checking if a media file exists using WebWorks (BB10)

That seems to be a bug with the invocation framework. Pretty sure I've seen it logged already.

 

The io.file and io.dir extensions are for PlayBook. For BB10 use the IO extension:

https://developer.blackberry.com/html5/apis/blackberry.io.html

 

This API works with the standard HTML5 file api and just fills in the specific BlackBerry pieces.

Tim Windsor
Application Development Advisor II
Please use plain text.
Developer
DueNorth
Posts: 135
Registered: ‎09-25-2012
My Device: Dev Alpha
My Carrier: Rogers

Re: Checking if a media file exists using WebWorks (BB10)

[ Edited ]

Hi Tim,

 

The BB10 IO extension creates a brand new file system where the files I want to read won't be inside. I guess it could work if there is a way to load all the images in. What if I want it to loop through the existing images directory and only show ones that exists?

 

for (var i = 1; i<100; i++) {
 var imagepath = "images/image"+i+".png";

 if(blackberry.io.file.exists(imagepath)){
 ... show image...
 }
}

It seems like blackberry.io.file.exits would work great. Is there an equivalent for BB10?

 

To elaborate further, all the HTML5 solutions requires me to input the file either by selecting them or dropping them into a div. There is no easy way to check if a file exists. I assume this is because of security.

Please use plain text.
BlackBerry Development Advisor
twindsor
Posts: 820
Registered: ‎07-15-2008
My Device: Z10
My Carrier: Bell

Re: Checking if a media file exists using WebWorks (BB10)

Start from code like this:

 

<script type="text/javascript">
function readFile() {
    // un-sandbox file system to access shared folder
    blackberry.io.sandbox = false;

    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

    window.requestFileSystem(window.TEMPORARY, 1024 * 1024,
        function (fs) {
    		fs.root.getDirectory(blackberry.io.sharedFolder + '/camera', {create: false},
    			function (directoryEntry) {
    				var reader = directoryEntry.createReader();
    				reader.readEntries(
    					function (entries) {
    						console.log(entries)
    					},
    					errorHandler);
    			});
        });
}

function errorHandler(e) {
    var msg = '';

    switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
        msg = 'QUOTA_EXCEEDED_ERR';
        break;
    case FileError.NOT_FOUND_ERR:
        msg = 'NOT_FOUND_ERR';
        break;
    case FileError.SECURITY_ERR:
         msg = 'SECURITY_ERR';
         break;
    case FileError.INVALID_MODIFICATION_ERR:
         msg = 'INVALID_MODIFICATION_ERR';
         break;
    case FileError.INVALID_STATE_ERR:
         msg = 'INVALID_STATE_ERR';
         break;
    default:
         msg = 'Unknown Error';
        break;
    };

    console.log('Error: ' + msg);
}

window.addEventListener('load', function() {
	document.addEventListener('webworksready', function() {
		readFile();
	})
});
</script>

 Then the "entries" array will hold your files and directories. You can use the DirectoryEntry methods (and FileEntry methods) on them.

Tim Windsor
Application Development Advisor II
Please use plain text.
Developer
DueNorth
Posts: 135
Registered: ‎09-25-2012
My Device: Dev Alpha
My Carrier: Rogers

Re: Checking if a media file exists using WebWorks (BB10)

[ Edited ]

Thanks Tim for the code. I should have said that I have tried the fileEntry method (from your example link) with no success.

 

Nothing happens in the console (with the path changed). 

 

Everything works fine to:

    		fs.root.getDirectory(blackberry.io.sharedFolder + '/images', {create: false},
    			function (directoryEntry) {
    				var reader = directoryEntry.createReader();
    				reader.readEntries(
    					function (entries) {
    						console.log(entries)
    					},
    					errorHandler);
    			});

However it looks like the function (directoryEntry) part isn't working. It didn't even trigger the errorHandler. If I put another function there, it triggers.

 

When I just do a console.log(blackberry.io.sharedFolder), it shows it as /webworksBB10/sharedFolder. I don't believe this maps to the application's root directory. What if I just want to read from the application's image folder (/image)? or root?

 

E.g.

 

config.xml

index.html

\images  <- I want to read from this folder

\js

 

Am I using it totally wrong?

Please use plain text.
BlackBerry Development Advisor (Retired)
haahmad
Posts: 6
Registered: ‎10-08-2012
My Device: developer
My Carrier: Telus

Hi Due North,    The sharedFolder variable does not point...

Hi Due North, 

 

The sharedFolder variable does not point to a webworks application's root folder, but the sharedFolder that all BB10 applications can access (if they have permission)

 

If you want access to your application's root folder you can use the "local:///" URL scheme which points to paths relative to your application's root directory.

Please use plain text.