Monotouch copy item from album (copy files from album)

Source: Internet
Author: User

The project needs to collect videos, photos, and recordings and upload them to the server. This requires reading these file streams, and the photos and recordings are easily handled.

The video is a little troublesome because the recorded video is saved in the album and then in finishedpickingmedia, unlike the photo, you can get it directly using the following code.

Uiimage image = (uiimage) info. objectforkey (New nsstring ("uiimagepickercontrolleroriginalimage "));

Therefore, if you want to directly read the album file, the test is successful in the simulator.

   1: public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)

   2: {

3: // you can read the image. Here we mainly test the video. below is the video address in the album, which is easily obtained by code.

4: // select a video in the simulator. After you click use, this event cannot be triggered. I don't know if it is a simulator bug. So fix the following video address and select an image to execute the following code.

   5:     NSUrl referenceURL = new NSUrl("assets-library://asset/asset.mov?id=5F4A9F73-3542-469F-8DAD-BC2B53BFB40C&ext=mov");

   6:     if (referenceURL != null)

   7:     {

   8:         ThreadPool.QueueUserWorkItem(delegate

   9:         {

  10:             ALAssetsLibrary library = new ALAssetsLibrary();

  11:             library.AssetForUrl(referenceURL, (asset) =>

  12:             {

  13:                 if (asset != null)

  14:                 {

  15:                     string filePath1 = Path.Combine(destPath, asset.DefaultRepresentation.Filename);

  16:                     long size = asset.DefaultRepresentation.Size;

  17:                     byte[] imgData = new byte[size];

  18:                     NSError nsError;

  19:                     IntPtr buffer = Marshal.AllocHGlobal(imgData.Length);

20: // copy a certain byte of alasset to a buffer

  21:                     asset.DefaultRepresentation.GetBytes(buffer, 0, (uint)size, out nsError);

22: write buffer to byte

  23:                     Marshal.Copy(buffer, imgData, 0, imgData.Length);

24: // you can write files or send byte streams.

  25:                     File.WriteAllBytes(filePath1, imgData);

  26:                 }

  27:  

  28:             }, (error) =>

  29:             {

  30:                 if (error != null)

  31:                 {

32: console. writeline ("error:" + error. localizeddescription );

  33:                 }

  34:             });

  35:         });

  36:     }

  37:     picker.DismissModalViewControllerAnimated(true);

  38: }

By Bruce Lee
Source: http://www.cnblogs.com/BruceLee521
This blog Original article is copyrighted by the blog and I share it with you. You are welcome to reprint it. However, you must keep this statement without the author's consent and provide the author name and original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.

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.