• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Loading images from local folder

Community Beginner ,
Mar 03, 2024 Mar 03, 2024

Copy link to clipboard

Copied

I have images from a local folder that I would like to load into my flash presentation as an array.

I'm trying to do an Actionscript code. Is there anyway to do that?

Views

730

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Mar 04, 2024 Mar 04, 2024
if all those bitmaps are in a subfolder (named bmp_folder) of your published files:
 
var load_targetA:Array = ["Alaska.png", "Australia.png"];
var loaderA = [];  // this will contain the loaders that load the above bitmaps
var index:int = 0;
load_nextF();
 
function load_nextF():void{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, load_completeF);
loader.load(new URLRequest("bmp_foloder/"+load_targetA[index]));
loaderA.push(loader);
}
 
function
...

Votes

Translate

Translate
Community Expert , Mar 09, 2024 Mar 09, 2024

Alright.

Please try this:

import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.filesystem.File;
import flash.net.URLRequest;

var source:String = "./images"; // the source directory must be included in the AIR settings
var extensions:Vector.<String> = new <String>[ "bmp", "jpeg", "jpg", "png" ];
var parsedSource:String;
var foundImages:Array;
var 
...

Votes

Translate

Translate
Community Expert ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

yes.  what problem are you having?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

I would like to know how to load local file images into an array and put it in the UILoader.

I tried to do the File function, but I think I'm doing something wrong.

Is there any way I can code it using Actionscript?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

what are the file names?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

"Alaska.png" "Australia.png"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

then you would need to add those names to an array, start loading the first, listen for loading to complete, advance to the next etc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

Ok. How can I code it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

do you know how to load one file and listen for completion of loading?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

no i do not.

I know how to load a file, but not how to listen for completion of loading

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

if all those bitmaps are in a subfolder (named bmp_folder) of your published files:
 
var load_targetA:Array = ["Alaska.png", "Australia.png"];
var loaderA = [];  // this will contain the loaders that load the above bitmaps
var index:int = 0;
load_nextF();
 
function load_nextF():void{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, load_completeF);
loader.load(new URLRequest("bmp_foloder/"+load_targetA[index]));
loaderA.push(loader);
}
 
function load_completeF(e:Event):void{
index++;
if(index<load_targetA.length){
load_nextF();
// and you may, or may not want to do something here
} else {
// all loading is complete. 
}
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

Ok that should work. Can I use URLRequest for the file location?
If so, how to load the contents?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

what do you mean by "can i use urlrequest for the file location"?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

Oh, I meant URLLoader. Sorry for the confusion.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

Hi.

 

Do you want to:
- Load all the files of any folder without knowing what's inside;
- Use a data file (.json, .xml...) that has the list of files you want to load;
- Simple declare an array/vector in AS3 code listing the files?

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

I would like to load all the files of any folder and declare an array in AS3 code listing the files.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

no, you would need to supply the file names unless there's some logic that could be used to generate those names.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

Ok. I'll give it a try.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

how many files are there?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

Alright.

Please try this:

import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.filesystem.File;
import flash.net.URLRequest;

var source:String = "./images"; // the source directory must be included in the AIR settings
var extensions:Vector.<String> = new <String>[ "bmp", "jpeg", "jpg", "png" ];
var parsedSource:String;
var foundImages:Array;
var loadedImages:Vector.<Bitmap>;

function main():void
{
	parsedSource = parsePath(source);
	
	foundImages = listDir(parsedSource).filter(function(element:*, index:int, arr:Array):Boolean
	{
		return extensions.indexOf(element.extension) > -1;
	});

	if (foundImages.length > 0)
	{
		loadedImages = new Vector.<Bitmap>();
		loadImages(foundImages);
	}
}

function parsePath(path:String):String
{
	const SEP:String = File.separator;
	
	var forwardReg:RegExp = /\//g;
	var backwardReg:RegExp = /\\/g;
	var path:String;

	path = path
		.replace(forwardReg, SEP)
		.replace(backwardReg, SEP);

	return path;
}

function loadImages(images:Array):void
{
	var i:uint;
	var total:uint = images.length;

	for (i = 0; i < total; i++)
		loadImage(images[i]);
}

function listDir(dir:String):Array
{
	var desktop:File = File.applicationDirectory.resolvePath(dir);
	return desktop.getDirectoryListing();
}

function loadImage(file:File):void
{
	var loader:Loader = new Loader();

	loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
	loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
	loader.load(new URLRequest(file.url));
}

function completeHandler(e:Event):void
{
	e.currentTarget.removeEventListener(e.type, arguments.callee);
	
	loadedImages.push(Bitmap((e.currentTarget as LoaderInfo).content));
	
	if (loadedImages.length == foundImages.length)
		displayImageGrid(loadedImages);
}

function ioErrorHandler(e:IOErrorEvent):void
{
	trace(e);
	e.currentTarget.removeEventListener(e.type, arguments.callee);
}
	
function securityErrorHandler(e:SecurityError):void
{
	trace(e);
}

function displayImageGrid(images:Vector.<Bitmap>):void
{
	var i:uint;
	var total:uint = images.length;
	var image:Bitmap;
	var baseWidth:uint = 256;
	var baseHeight:uint = 384;
	var gapX:int = 10;
	var gapY:int = 10;
	var columns:uint = uint(stage.stageWidth / (baseWidth + gapX));
	
	for (i = 0; i < total; i++)
	{
		image = images[i];
		image.width = baseWidth;
		image.scaleY = image.scaleX;
		image.x = gapX + (i % columns) * (baseWidth + gapX);
		image.y = gapY + int(i / columns) * (baseHeight + gapY);
		image.smoothing = true;
		addChild(image);
	}
}

main();

 

And please notice that the source directory must be included in the AIR settings.

image.png

 

I hope this helps.

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

how do you know this is an air app?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

@kglad: The OP said it's a Flash presentation. So why not?

 

Also, using AIR is the only way to list a folder's content.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

I keep getting this error. "1046: Type was not found or was not a compile-time constant: File."

But, it overall works.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

@Caleb Washington 

 

then it's not an ait app?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

what line of code is triggering that error?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2024 Mar 09, 2024

Copy link to clipboard

Copied

Line 53

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines