예제 #1
0
        private void ExportScanData()
        {
            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted ||
                ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == (int)Permission.Granted)
            {
                List <ParcelScans> parcelScans = databaseConnection.Query <ScanSKUDataBase.ParcelScans>("SELECT * FROM ParcelScans");

                string fileName = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".csv";
                // Set a variable to the Documents path.
                string docPath  = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
                string filepath = (Path.Combine(docPath, fileName));
                using (StreamWriter outputFile = new StreamWriter(filepath))
                {
                    foreach (ParcelScans parcelScan in parcelScans)
                    {
                        outputFile.WriteLine(parcelScan.ToCSV());
                    }
                }

                // Notify the user about the completed "download"
                DownloadManager downloadManager = DownloadManager.FromContext(Android.App.Application.Context);
                downloadManager.AddCompletedDownload(fileName, "Barcode Scan Data Export", true, "application/txt", filepath, File.ReadAllBytes(filepath).Length, true);
            }
            else
            {
                ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage, Manifest.Permission.ReadExternalStorage }, 2);
            }
        }
예제 #2
0
        public void FileReceivedDelegate(string fileName, string fileDescription, string filePath, int fileSize, TextView associatedTextView)
        {
            MimeTypeMap mime      = MimeTypeMap.Singleton;
            string      ext       = fileName.ToLower();
            string      extension = MimeTypeMap.GetFileExtensionFromUrl(ext);
            string      type      = null;

            if (extension != null)
            {
                type = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
            }
            if (type == null)
            {
                type = "application/octet-stream";
            }

            DownloadManager downloadManager = DownloadManager.FromContext(Android.App.Application.Context);

            downloadManager.AddCompletedDownload(fileName, fileDescription, true, type, filePath, fileSize, true);

            CommClientAndroid client2 = ClientHolder.Client;

            client2.RemoveFile(associatedTextView.Text);
            ViewGroup parent = (ViewGroup)associatedTextView.Parent;

            parent.Post(delegate
            {
                parent.RemoveView(associatedTextView);
            });

            client.currentlyDownloadingFile = false;
        }
        public void MakeDownloadFolder(string fullFileName, string mimeType)
        {
            var filePath = fullFileName;
            var fileName = Path.GetFileName(fullFileName);

            Java.IO.File file = new Java.IO.File(fullFileName);

            var             context         = Android.App.Application.Context;
            DownloadManager downloadManager = (DownloadManager)context.GetSystemService(Context.DownloadService);

            downloadManager.AddCompletedDownload(fileName, fileName, true, mimeType, fullFileName, file.Length(), true);
        }
예제 #4
0
        /// <summary>
        /// This will not be used in iOS
        /// </summary>
        /// <param name="fileDir">File directory</param>
        /// <param name="fileName">File name</param>
        /// <param name="fileDescription">Description to be used to add to the download manager</param>
        /// <returns>status - true: worked; false: failure</returns>
        public bool SetDownload(string fileDir, string fileName, string fileDescription)
        {
            bool         permissionGranted = false;
            const string mimeUriFile       = "file://";
            const string excelMimeType     = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            Version version = DeviceInfo.Version;

            // This should not be used at or after version 8 (API 26)
            if (version.Major >= 8)
            {
                return(true);
            }

            try
            {
                var context = Android.App.Application.Context;
                permissionGranted = true;
                string   fileSpec = Path.Combine(fileDir, fileName);
                FileInfo fileInfo = new FileInfo(fileSpec);
                if (fileInfo.Exists)
                {
                    // Notify the user about the completed "download"
                    DownloadManager downloadManager = DownloadManager.FromContext(context);

                    // https://www.sandersdenardi.com/using-the-android-downloadmanager/
                    // https://docs.microsoft.com/en-us/dotnet/api/android.app.downloadmanager.invokequery?view=xamarin-android-sdk-9#Android_App_DownloadManager_InvokeQuery_Android_App_DownloadManager_Query_
                    // have we already added the file to the download manager?
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.SetFilterByStatus(DownloadStatus.Pending | // <- note bitwise OR
                                            DownloadStatus.Running |
                                            DownloadStatus.Successful);

                    ICursor cursor = downloadManager.InvokeQuery(query);

                    int colDescrption = cursor.GetColumnIndex(DownloadManager.ColumnDescription);
                    int colDirectory  = cursor.GetColumnIndex(DownloadManager.ColumnLocalUri);
                    int colMimeType   = cursor.GetColumnIndex(DownloadManager.ColumnMediaType);
                    int colFileName   = cursor.GetColumnIndex(DownloadManager.ColumnTitle);

                    bool exactMatchFile = false;
                    bool exactMatchDir  = false;
                    bool exactMatchDesc = false;

                    for (cursor.MoveToFirst(); !cursor.IsAfterLast; cursor.MoveToNext())
                    {
                        string dmFileName = cursor.GetString(colFileName);
                        if (string.IsNullOrEmpty(dmFileName))
                        {
                            dmFileName = string.Empty;
                        }

                        if (dmFileName.Equals(fileName))
                        {
                            exactMatchFile = true;
                        }

                        string dmDesc = cursor.GetString(colDescrption);
                        if (string.IsNullOrEmpty(dmDesc))
                        {
                            dmDesc = string.Empty;
                        }

                        if (dmDesc.Equals(fileDescription))
                        {
                            exactMatchDesc = true;
                        }

                        string dmMimeType = cursor.GetString(colMimeType);
                        if (string.IsNullOrEmpty(dmMimeType))
                        {
                            dmMimeType = string.Empty;
                        }

                        string dmDirectory = cursor.GetString(colDirectory);
                        if (string.IsNullOrEmpty(dmDirectory))
                        {
                            dmDirectory = string.Empty;
                        }
                        else
                        {
                            int i = dmDirectory.IndexOf(mimeUriFile, StringComparison.Ordinal);
                            if (i >= 0)
                            {
                                dmDirectory = dmDirectory.Substring(mimeUriFile.Length);
                            }
                        }

                        if (dmDirectory.Equals(fileDir))
                        {
                            exactMatchDir = true;
                        }

                        if (exactMatchFile && exactMatchDir && exactMatchDesc)
                        {
                            break;
                        }
                    }

                    cursor.Close();
                    query = null;

                    if (!(exactMatchFile && exactMatchDir && exactMatchDesc))
                    {
                        downloadManager.AddCompletedDownload(fileInfo.Name, fileDescription, true, excelMimeType,
                                                             fileInfo.DirectoryName, fileInfo.Length, true);
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("SetDownload() filespec: " + fileDir +
                                                   " desc: " + fileDescription + " permission granted " + permissionGranted +
                                                   " - exception: " + ex.Message);
                System.Diagnostics.Debug.Flush();
            }

            return(false);
        }