Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPD: Copy images from Android using RunAsyncAction

$
0
0

Hi All:

I have a function to copy the images from Android devices & its working. But i have some issue

issues:

1. I want to display a 'Progressbar" while copying the images. But the prgress bar is displayed at the end.

2. How can i wrap-up the error message of disconnecting the cable while copying the images?

Function to copy the images from Android device

       private void bGetImages_Click(object sender, RoutedEventArgs e) // Get all the images from the device
        {
            try
            {
                DateTime start = DateTime.Now;
                devList = DNPWPD.WinPortableDeviceHelper.DetectDevices();
                RunAsyncAction(new Action(() =>
                {

                    //do it all....

                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        if (devList != null && devList.Count > 0)
                        {
                            WaitBar.Visibility = System.Windows.Visibility.Visible;
                            string name = DNWPD.WinPortableDeviceHelper.GetDeviceFriendlyName(devList[0]);

                            listBox1.Items.Add("Device found: " + name);


                            List<string> fTypes = new List<string>();
                            fTypes.Add(".JPg");
                            fTypes.Add(".Bmp");
                            fTypes.Add(".Tif");
                            fTypes.Add(".png");
                            fTypes.Add(".mov");

                            content = WinPortableDeviceHelper.IndexFiles(devList[0], fTypes);// .GetFileList(devList[0], fTypes);
                            foreach (WinPortableDeviceHelper.ObjectInfo file in content)
                            {
                                listBox1.Items.Add("Found image object: " + file.imagename);
                            }
                            fTypes.Clear();

                            if (content == null || content.Count <= 0)
                                MessageBox.Show("There is no content to retrieve.  Did you List Devices and Get Image List first?");
                            else
                            {
                                if (Directory.Exists(".\\Device"))
                                    Directory.Delete(".\\Device");

                                if (!Directory.Exists(".\\Device"))
                                    Directory.CreateDirectory(".\\Device");

                                // Code for thumbnail.
                                //for (int i = 0; i < content.Count(); i++)
                                //{
                                //    WinPortableDeviceHelper.ObjectInfo wpdObject = content[i];
                                //    WinPortableDeviceHelper.GetFileData(devList[0], ref wpdObject);
                                //}

                                for (int i = 0; i < content.Count(); i++)
                                {
                                    WinPortableDeviceHelper.ObjectInfo wpdObject = content[i];
                                    string contentPath = "";
                                    FileStream fs = WinPortableDeviceHelper.GetFile(devList[0], wpdObject.objid, out contentPath);
                                    fs.Close();
                                    fs = null;
                                }

                                TimeSpan ts = DateTime.Now - start;
                                MessageBox.Show("Retrieved " + content.Count + " images in " + ts.ToString());
                            }

                        }

                    }));
                }), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Conneciton lost", ex.ToString());
            }


        }

Function of RunAysnc:

 private void RunAsyncAction(Action action, Action uiAction = null)
        {
            WaitBar.Visibility = System.Windows.Visibility.Visible;
            Task.Factory.StartNew(() =>
            {
                try
                {
                    action();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }).ContinueWith(parent =>
            {
                WaitBar.Visibility = System.Windows.Visibility.Collapsed;
                if (uiAction != null) uiAction();
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }

Thanks for your help.

KR


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>