public static async Task<Android.Net.Uri> DownloadDocument (Context ctx, Android.Net.Uri uriToDownload, string saveFilePath, IProgress<DownloadBytesProgress> progessReporter)
		{
			int receivedBytes = 0;
			int totalBytes = 0;

			using (var assetFileDescriptor = ctx.ContentResolver.OpenAssetFileDescriptor (uriToDownload, "r"))
			using (var fileOutputStream = new Java.IO.FileOutputStream (saveFilePath))
			using (var inputStream = assetFileDescriptor.CreateInputStream ()) {
				var buffer = new byte [BufferSize];
				totalBytes = (int)assetFileDescriptor.Length;

				for (;;) {
					var bytesRead = await inputStream.ReadAsync (buffer, 0, buffer.Length);
					if (bytesRead == 0) {
						await Task.Yield();
						break;
					}
					await fileOutputStream.WriteAsync (buffer, 0, buffer.Length);

					receivedBytes += bytesRead;
					if (progessReporter != null) {
						var args = new DownloadBytesProgress (uriToDownload.ToString (), saveFilePath, receivedBytes, totalBytes);
						progessReporter.Report(args);
					}
				}
				inputStream.Close ();
				fileOutputStream.Close ();
			}
			var file = new Java.IO.File (saveFilePath);
			var docUri = Android.Net.Uri.FromFile (file);
			return docUri;
		}
        private async Task <string> SaveFileToFolder(Android.Net.Uri uri, string fileName)
        {
            Android.Net.Uri docUri = Android.Provider.DocumentsContract.BuildDocumentUriUsingTree(uri,
                                                                                                  Android.Provider.DocumentsContract.GetTreeDocumentId(uri));

            string fileFolder = SAFFileUtil.GetPath(this, docUri);

            Java.IO.File destFolder = new Java.IO.File(fileFolder);
            Java.IO.File file       = new Java.IO.File(destFolder, fileName);
            Java.IO.File tempFile   = new Java.IO.File(TempFilePath);
            destFolder.Mkdirs();
            if (!file.Exists())
            {
                file.CreateNewFile();
            }

            Java.IO.FileInputStream  fileInput  = new Java.IO.FileInputStream(tempFile);
            Java.IO.FileOutputStream fileOutput = new Java.IO.FileOutputStream(file, false);

            while (true)
            {
                byte[] data = new byte[1024];
                int    read = await fileInput.ReadAsync(data, 0, data.Length);

                if (read <= 0)
                {
                    break;
                }
                await fileOutput.WriteAsync(data, 0, read);
            }
            fileInput.Close();
            fileOutput.Close();

            return(file.AbsolutePath);
        }
Ejemplo n.º 3
0
        public static async Task <Android.Net.Uri> DownloadDocument(Context ctx, Android.Net.Uri uriToDownload, string saveFilePath, IProgress <DownloadBytesProgress> progessReporter)
        {
            int receivedBytes = 0;
            int totalBytes    = 0;

            using (var assetFileDescriptor = ctx.ContentResolver.OpenAssetFileDescriptor(uriToDownload, "r"))
                using (var fileOutputStream = new Java.IO.FileOutputStream(saveFilePath))
                    using (var inputStream = assetFileDescriptor.CreateInputStream()) {
                        var buffer = new byte [BufferSize];
                        totalBytes = (int)assetFileDescriptor.Length;

                        for (;;)
                        {
                            var bytesRead = await inputStream.ReadAsync(buffer, 0, buffer.Length);

                            if (bytesRead == 0)
                            {
                                await Task.Yield();

                                break;
                            }
                            await fileOutputStream.WriteAsync(buffer, 0, buffer.Length);

                            receivedBytes += bytesRead;
                            if (progessReporter != null)
                            {
                                var args = new DownloadBytesProgress(uriToDownload.ToString(), saveFilePath, receivedBytes, totalBytes);
                                progessReporter.Report(args);
                            }
                        }
                        inputStream.Close();
                        fileOutputStream.Close();
                    }
            var file   = new Java.IO.File(saveFilePath);
            var docUri = Android.Net.Uri.FromFile(file);

            return(docUri);
        }