private void ProcessResponse(Stream stream)
        {
            try
            {
                var fileService  = MvxFileStoreHelper.SafeGetFileStore();
                var tempFilePath = this.DownloadPath + ".tmp";

                fileService.WriteFile(
                    tempFilePath,
                    (fileStream) =>
                {
                    var buffer = new byte[4 * 1024];
                    int count;
                    while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fileStream.Write(buffer, 0, count);
                    }
                });

                fileService.TryMove(tempFilePath, this.DownloadPath, true);
            }
            catch (Exception exception)
            {
                this.FireDownloadFailed(exception);
                return;
            }

            this.FireDownloadComplete();
        }
Exemplo n.º 2
0
        private void HandleSuccess(Task <Stream> result)
        {
            try
            {
                var fileService  = MvxFileStoreHelper.SafeGetFileStore();
                var tempFilePath = DownloadPath + ".tmp";

                using (result.Result)
                {
                    fileService.WriteFile(tempFilePath,
                                          (fileStream) =>
                    {
                        result.Result.CopyTo(fileStream);
                    });
                }
                fileService.TryMove(tempFilePath, DownloadPath, true);
            }
            catch (Exception exception)
            {
                FireDownloadFailed(exception);
                return;
            }

            FireDownloadComplete();
        }
        public MvxDynamicCompressedBitmapHelper()
        {
            if (!_isCacheInitialized)
            {
                var fileService = MvxFileStoreHelper.SafeGetFileStore();
                fileService.EnsureFolderExists(PicCacheDir);

                // Remove old cached files
                var cacheFiles = fileService.GetFilesIn(PicCacheDir);
                if (cacheFiles != null)
                {
                    var expiredDay = DateTime.Now.AddDays(-CacheFileExpirationDays);
                    foreach (var file in cacheFiles)
                    {
                        var createdAt = File.GetCreationTime(file);
                        if (createdAt < expiredDay)
                        {
                            fileService.DeleteFile(file);
                        }
                    }
                }

                if (_localFilesCache == null)
                {
                    _localFilesCache = new ConcurrentDictionary <string, Bitmap>();
                }

                if (_remoteFilesCache == null)
                {
                    _remoteFilesCache = new ConcurrentDictionary <string, CacheItem>();
                }

                _isCacheInitialized = true;
            }
        }
        private async Task <Bitmap> GetBitmap(string imageSource)
        {
            Bitmap image = null;

            var fileName = imageSource.Substring(imageSource.LastIndexOf('/') + 1);

            if (!fileName.Contains("."))
            {
                fileName += ".bmp";
            }

            var fullPath = $"{PicCacheDir}{fileName}";

            var fileService = MvxFileStoreHelper.SafeGetFileStore();

            if (fileService.Exists(fullPath))
            {
                byte[] storedFile;
                fileService.TryReadBinaryFile(fullPath, out storedFile);

                if (storedFile != null && storedFile.Length > 0)
                {
                    try
                    {
                        image = BitmapFactory.DecodeByteArray(storedFile, 0, storedFile.Length);
                    }
                    catch (Exception ex)
                    {
                        Mvx.Error(ex.Message);
                    }
                }
            }
            else
            {
                var restClient = Mvx.Resolve <RestClient>();
                try
                {
                    var data = await restClient.DownloadContent(imageSource).ConfigureAwait(false);

                    fileService.WriteFile(fullPath, data);
                    image = BitmapFactory.DecodeByteArray(data, 0, data.Length);
                }
                catch (Exception ex)
                {
                    Mvx.Error(ex.Message);
                }
            }

            return(image);
        }
Exemplo n.º 5
0
        private void ProcessResponse(byte[] array)
        {
            try
            {
                var fileService  = MvxFileStoreHelper.SafeGetFileStore();
                var tempFilePath = DownloadPath + ".tmp";

                fileService.WriteFile(tempFilePath, array);

                fileService.TryMove(tempFilePath, DownloadPath, true);
            }
            catch (Exception ex)
            {
                FireDownloadFailed(ex);
                Mvx.Resolve <MvvmCross.Platform.Platform.IMvxTrace>().Trace(MvvmCross.Platform.Platform.MvxTraceLevel.Error, $"{GetType().FullName}\n", ex.BuildAllMessagesAndStackTrace());
                return;
            }

            Mvx.Resolve <IMvxMainThreadDispatcher>().RequestMainThreadAction(FireDownloadComplete);
        }