Urlstream, urlloader, and loader

Source: Internet
Author: User

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 ):

  1. VaR BA: bytearray = new bytearray;
  2. Ba.writebytes(event.tar get as urlloader). Data );
  3. Ba. Position = 0;
  4. // Assume that an XML document is recorded.
  5. 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:

  1. // Import related
  2. Import MX. Events. closeevent;
  3. Import flash. Geom. matrix;
  4. Import MX. Graphics. codec. jpegencoder;
  5. Import MX. Graphics. codec. pngencoder;
  6. Import MX. Controls. image;
  7. Import MX. Controls. Alert;
  8. Import MX. utils. arrayutil;
  9. Import MX. Collections. arraycollection;
  10. Import MX. formatters. numberformatter;
  11. Import MX. messaging. abstractconsumer;
  12. // Compress the information of the thumbnail.
  13. Private var smallpicsize: Int = 80;
  14. // Save the photo extension list
  15. Private Static const ext_jpeg: String = "jpg ";
  16. Private Static const ext_png: String = "PNG ";
  17. Public Function browseandupload (): void
  18. {
  19. VaR filetoopen: file = file.doc umentsdirectory;
  20. VaR txtfilter: filefilter = new filefilter ("JPG, PNG", "*. jpg; * PNG ;");
  21. Filetoopen. browseforopen ("open", [txtfilter]);
  22. Filetoopen. addeventlistener (event. Select, filetoopenselected );
  23. }
  24. // When the uploaded photo is opened, upload the photo
  25. Private function filetoopenselected (objevent: Event): void
  26. {
  27. VaR orgimgpath: String = file (objevent. currenttarget). nativepath;
  28. Try
  29. {
  30. VaR orgfile: file = new file (orgimgpath );
  31. // Obtain a random image name
  32. VaR curpicname: String = "testuploadpic." + orgfile. extension;
  33. // If we put the large image in the project's/upload/big/folder
  34. VaR bigimgfile: file = new file (file. applicationdirectory. nativepath + "/upload/big/" + curpicname );
  35. Orgfile. copyto (bigimgfile );
  36. // Obtain the bitmap information of the source image, and reconstruct a small image and place it under the temp/small/folder.
  37. VaR Loader: loader = new loader ();
  38. // Use an inline function to pass parameters in an event
  39. VaR myspecialobject: Object = {newimgname: curpicname, newimgext: orgfile. Extension };
  40. // Note: loader. contentloaderinfo
  41. Loader. contentloaderinfo. addeventlistener (event. Complete, function (E: Event): void {loadcomplete (E, myspecialobject );});
  42. Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, loaderror );
  43. // Load the source Image
  44. VaR request: URLRequest = new URLRequest (orgfile. nativepath );
  45. Loader. Load (request );
  46. }
  47. Catch (ER: Error)
  48. {
  49. Alert. Show (ER. Message );
  50. }
  51. }
  52. // 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
  53. Public Function loadcomplete (Event: event, objspecial: Object): void
  54. {
  55. Try
  56. {
  57. If (objspecial! = NULL & objspecial. newimgname. tostring ()! = "")
  58. {
  59. VaR Loader: loader = loader(event.tar get. loader );
  60. VaR bigbitmapdata: bitmapdata = Bitmap (loader. contentloaderinfo. Content). bitmapdata;
  61. VaR myscale: Number = 1.0;
  62. If (bigbitmapdata. width> 120 | bigbitmapdata. Height> 100)
  63. {
  64. If (bigbitmapdata. width> bigbitmapdata. Height)
  65. {
  66. Myscale = 120/bigbitmapdata. width;
  67. } Else
  68. {
  69. Myscale = 100/bigbitmapdata. height;
  70. }
  71. }
  72. // Create a new conversion matrix
  73. VaR M: matrix = new matrix ();
  74. M. Scale (myscale, myscale );
  75. VaR smallbitmapdata: bitmapdata = new bitmapdata (bigbitmapdata. Width * myscale, bigbitmapdata. Height * myscale, bigbitmapdata. Transparent, 0 xffffffff );
  76. Smallbitmapdata. Draw (bigbitmapdata, M );
  77. // Bytearray of the photo
  78. VaR imgbytearray: bytearray;
  79. Switch (objspecial. newimgext. tostring ())
  80. {
  81. Case ext_jpeg:
  82. VaR jpgenc: incluencoder = new incluencoder (80 );
  83. Imgbytearray = jpgenc. encode (smallbitmapdata );
  84. Break;
  85. Case ext_png:
  86. VaR pngenc: pngencoder = new pngencoder ();
  87. Imgbytearray = pngenc. encode (smallbitmapdata );
  88. Break;
  89. }
  90. // If we put the large image in the project's/upload/small/folder
  91. VaR smallfile: file = new file (file. applicationdirectory. nativepath + "/upload/small/" + objspecial. newimgname. tostring ());
  92. VaR FS: filestream = new filestream ();
  93. Try {
  94. FS. Open (smallfile, filemode. Write );
  95. FS. writebytes (imgbytearray );
  96. FS. Close ();
  97. } Catch (E: Error ){
  98. Alert. Show (E. Message );
  99. }
  100. // Release the memory occupied by bitmapdata (important)
  101. Bigbitmapdata. Dispose ();
  102. Smallbitmapdata. Dispose ();
  103. }
  104. }
  105. Catch (ERR: Error)
  106. {
  107. Alert. Show ("An error occurred while generating the small image .");
  108. }
  109. }
  110. // Handle errors when loading the source Image
  111. Public Function loaderror (Event: ioerrorevent): void
  112. {
  113. Alert. Show ("uploaded Image Source:" uploloader(event.tar get. loader). contentloaderinfo. loaderurl. tostring () + "");
  114. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.