This one has been bugging me for a few days, and despite the wonderfulness of the
File extension from Taifun, I have been searching for a way to test for the existence of an image file using just AI2 blocks and components.
Why do you need this? (for whatever reason you don't want to use an extension
You may have a situation where you download image files to the app when the user visits a particular part of the app.
Once downloaded, it makes more sense to use the image on the device, as opposed to downloading it again.
My solution was to use a little bit of javascript, a webviewer with webviewstring, a clock and some logic.
Works with webviewer not visible as well
I also used the file component to install the html file needed to the sdcard
Default location is file:///mnt.sdcard, but subdirectories work too, if the additional path is added
My example simply check for an image file, and if it exists displays it, along with a message.
If the file doesn't exist, displays a different "Sorry" image in the app, and a message.
Developers will be able to apply their own logic, so that if the file is not on the device, it
can be downloaded from online resources.
Most of the components used are likely to already be in an app whilst developing, so often simply a matter of reuse
BLOCKS
Please note, in production, for the filepath for the initial webviewstring and for the "else" setting of Image1.Picture
you may not need to include "file:///mnt/sdcard".
Also you can create a specific directory on the sdcard when you create the html file in Screen Initialise.
HTML
<!DOCTYPE html >
<html>
<head>
<title>Test Image Exists</title>
</head>
<body>
<script>
function checkImage(imageSrc, good, bad) {
var img = new Image();
img.onload = good;
img.onerror = bad;
img.src = imageSrc;
}
checkImage(window.AppInventor.getWebViewString(),
function(){ window.AppInventor.setWebViewString("File exists"); },
function(){ window.AppInventor.setWebViewString("File doesn't exist"); } );
</script>
</body>
</html>
Credits: in html
aia attached :)
Enjoy!