Reading JPG in BitmapData in ActionScript 3 (JPG is inside the APK file)

I am using Adobe AIR to create an APK file for my Android phone. Inside the APK package there is a SWF file and in the same folder as the SWF file, there is a subfolder named "images" that contains image.jpg.

I am trying to read this image.jpg in a program at runtime, but cannot get its location (the program does not seem to find it.

Here is what I am trying:

var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); loader.load(new URLRequest("images/image.jpg")); while (bitmapData==null) {} //wait until loaded return bitmapData; 

The onComplete function puts sets the bitmapData field. However, the COMPLETE event never occurs, so the while loop never exists.

I also tried:

 loader.load(new URLRequest("/images/image.jpg")); loader.load(new URLRequest("app:/images/image.jpg")); loader.load(new URLRequest("app-storage:/images/image.jpg")); loader.load(new URLRequest("file:/images/image.jpg")); 

None of this works.

I can not get this to work even if I try without an APK file when I run the created SWF file (the subfolder of the images is in the same folder as the SWF file).

I cannot embed JPG in the SWF file itself because I need to have many image files, and Flash CS6 runs out of memory when creating such a large SWF file (reading images from the outside also means much less collection time).

Any ideas? How can I put an external jpg file in BitmapData? Remember that the image is not outside the APK file, it is packed inside it.

Note. I also export the thing to the iOS iOS package later, so it should work there as well.


Change, because I can not answer on my own because of my reputation.

Strille made me realize that the ActionScript virtual machine is single-threaded.

Thus, it is not possible to create a stream (wait) for the uploaded image. Instead, I have to sigh, rewriting a lot of code so that things stop and continue when the onComplete function is called.

This thing I tried before it works, but cannot complete due to the while loop:

 loader.load(new URLRequest("app:/images/image.jpg")); 
+3
source share
2 answers

If I remember correctly, you should do it like this:

 loader.load(new URLRequest(new File("app:/images/image.jpg").url)); 

Since we use the File class, the above will not run in Flash Player, only on iOS or Android.

+1
source

From my experience there are two practical methods for loading assets from running Android APP using AIR

  • using the URLLoader and .url properties of the File instance
  • using FileStream (which receives an instance of File)

Adding to the project

You can pack the files inside the APK using the same technique as ICONS packaging inside the .xml descriptor file for your application.

And make sure you add them to your IDE in the Android section (applicable to IntelliJ and Flash Builder). “Files and folders for packaging” - add the path to the folder of the required assets and give it a relative alias for the name.

Development

Assets themselves are hosted (when the APP is installed), inside the " applicationDirectory ", which is available for AIR,

But since the new Android OS and problems with Flash Player, it has been impossible to access these resources through nativePath , only the url property of the File instance.

Best practice

Use the URLLoader and .url properties of the File instance, since it is also asynchronous in nature and can resolve PNG / JPG files using its own decoder instead of doing it manually by reading a FileStream (which returns a ByteArray )

 var f:File = File.applicationDirectory.resolvePath('images/myimage.png'); loader.load(new URLRequest(f.url)); 

and, If I miss other ways, let me know ...)

0
source

Source: https://habr.com/ru/post/1440984/


All Articles