Urlsteam supports multiple encoding methods to read text files.
If urlloader encounters garbled characters, it can be converted (as mentioned in the previous article, excle can solve the garbled problem based on the standard XML format ):
- VaR BA: bytearray = new bytearray;
- Ba.writebytes(event.tar get as urlloader). Data );
- Ba. Position = 0;
- // Assume that an XML document is recorded.
- VaR XML: xml = XML (BA. readmultibyte (BA. length, "GBK "));
Loader is used to load external image files and SWF files. I used it in air to upload images and generate a thumbnail. The implementation code is as follows:
- // Import related
- Import MX. Events. closeevent;
- Import flash. Geom. matrix;
- Import MX. Graphics. codec. jpegencoder;
- Import MX. Graphics. codec. pngencoder;
- Import MX. Controls. image;
- Import MX. Controls. Alert;
- Import MX. utils. arrayutil;
- Import MX. Collections. arraycollection;
- Import MX. formatters. numberformatter;
- Import MX. messaging. abstractconsumer;
- // Compress the information of the thumbnail.
- Private var smallpicsize: Int = 80;
- // Save the photo extension list
- Private Static const ext_jpeg: String = "jpg ";
- Private Static const ext_png: String = "PNG ";
- Public Function browseandupload (): void
- {
- VaR filetoopen: file = file.doc umentsdirectory;
- VaR txtfilter: filefilter = new filefilter ("JPG, PNG", "*. jpg; * PNG ;");
- Filetoopen. browseforopen ("open", [txtfilter]);
- Filetoopen. addeventlistener (event. Select, filetoopenselected );
- }
- // When the uploaded photo is opened, upload the photo
- Private function filetoopenselected (objevent: Event): void
- {
- VaR orgimgpath: String = file (objevent. currenttarget). nativepath;
- Try
- {
- VaR orgfile: file = new file (orgimgpath );
- // Obtain a random image name
- VaR curpicname: String = "testuploadpic." + orgfile. extension;
- // If we put the large image in the project's/upload/big/folder
- VaR bigimgfile: file = new file (file. applicationdirectory. nativepath + "/upload/big/" + curpicname );
- Orgfile. copyto (bigimgfile );
- // Obtain the bitmap information of the source image, and reconstruct a small image and place it under the temp/small/folder.
- VaR Loader: loader = new loader ();
- // Use an inline function to pass parameters in an event
- VaR myspecialobject: Object = {newimgname: curpicname, newimgext: orgfile. Extension };
- // Note: loader. contentloaderinfo
- Loader. contentloaderinfo. addeventlistener (event. Complete, function (E: Event): void {loadcomplete (E, myspecialobject );});
- Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, loaderror );
- // Load the source Image
- VaR request: URLRequest = new URLRequest (orgfile. nativepath );
- Loader. Load (request );
- }
- Catch (ER: Error)
- {
- Alert. Show (ER. Message );
- }
- }
- // The original image is also loaded into the memory during the upload process. The main purpose is to generate a small image using the original image. The processing method after the load operation is completed
- Public Function loadcomplete (Event: event, objspecial: Object): void
- {
- Try
- {
- If (objspecial! = NULL & objspecial. newimgname. tostring ()! = "")
- {
- VaR Loader: loader = loader(event.tar get. loader );
- VaR bigbitmapdata: bitmapdata = Bitmap (loader. contentloaderinfo. Content). bitmapdata;
- VaR myscale: Number = 1.0;
- If (bigbitmapdata. width> 120 | bigbitmapdata. Height> 100)
- {
- If (bigbitmapdata. width> bigbitmapdata. Height)
- {
- Myscale = 120/bigbitmapdata. width;
- } Else
- {
- Myscale = 100/bigbitmapdata. height;
- }
- }
- // Create a new conversion matrix
- VaR M: matrix = new matrix ();
- M. Scale (myscale, myscale );
- VaR smallbitmapdata: bitmapdata = new bitmapdata (bigbitmapdata. Width * myscale, bigbitmapdata. Height * myscale, bigbitmapdata. Transparent, 0 xffffffff );
- Smallbitmapdata. Draw (bigbitmapdata, M );
- // Bytearray of the photo
- VaR imgbytearray: bytearray;
- Switch (objspecial. newimgext. tostring ())
- {
- Case ext_jpeg:
- VaR jpgenc: incluencoder = new incluencoder (80 );
- Imgbytearray = jpgenc. encode (smallbitmapdata );
- Break;
- Case ext_png:
- VaR pngenc: pngencoder = new pngencoder ();
- Imgbytearray = pngenc. encode (smallbitmapdata );
- Break;
- }
- // If we put the large image in the project's/upload/small/folder
- VaR smallfile: file = new file (file. applicationdirectory. nativepath + "/upload/small/" + objspecial. newimgname. tostring ());
- VaR FS: filestream = new filestream ();
- Try {
- FS. Open (smallfile, filemode. Write );
- FS. writebytes (imgbytearray );
- FS. Close ();
- } Catch (E: Error ){
- Alert. Show (E. Message );
- }
- // Release the memory occupied by bitmapdata (important)
- Bigbitmapdata. Dispose ();
- Smallbitmapdata. Dispose ();
- }
- }
- Catch (ERR: Error)
- {
- Alert. Show ("An error occurred while generating the small image .");
- }
- }
- // Handle errors when loading the source Image
- Public Function loaderror (Event: ioerrorevent): void
- {
- Alert. Show ("uploaded Image Source:" uploloader(event.tar get. loader). contentloaderinfo. loaderurl. tostring () + "");
- }