예제 #1
0
        public string Save(MemoryStream stream)
        {
            string root     = null;
            string fileName = "SavedDocument.pdf";

            if (Android.OS.Environment.IsExternalStorageEmulated)
            {
                root = Android.OS.Environment.ExternalStorageDirectory.ToString();
            }
            Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion");
            myDir.Mkdir();
            Java.IO.File file     = new Java.IO.File(myDir, fileName);
            string       filePath = file.Path;

            if (file.Exists())
            {
                file.Delete();
            }
            Java.IO.FileOutputStream outs = new Java.IO.FileOutputStream(file);
            outs.Write(stream.ToArray());
            var ab = file.Path;

            outs.Flush();
            outs.Close();
            return(filePath);
        }
예제 #2
0
        private void PdfViewerControl_DocumentSaveInitiated(object sender, DocumentSaveInitiatedEventArgs args)
        {
            MemoryStream stream   = args.SavedStream as MemoryStream;
            string       root     = null;
            string       fileName = "sample.pdf";

            if (Android.OS.Environment.IsExternalStorageEmulated)
            {
                root = Android.OS.Environment.ExternalStorageDirectory.ToString();
            }
            else
            {
                root = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            }
            Java.IO.File directory = new Java.IO.File(root + "/Syncfusion");
            directory.Mkdir();
            Java.IO.File file = new Java.IO.File(directory, fileName);
            if (file.Exists())
            {
                file.Delete();
            }
            Java.IO.FileOutputStream outputStream = new Java.IO.FileOutputStream(file);
            outputStream.Write(stream.ToArray());
            outputStream.Flush();
            outputStream.Close();
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(mainView.Context);
            alertDialog.SetTitle("Save");
            alertDialog.SetMessage("The modified document is saved in the below location. " + "\n" + file.Path);
            alertDialog.SetPositiveButton("OK", (senderAlert, e) => { });
            Dialog dialog = alertDialog.Create();

            dialog.Show();
        }
예제 #3
0
        private void SavetoSd()
        {
            //var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
            //var filePath = System.IO.Path.Combine(sdCardPath, "iootext.txt");
            //if (!System.IO.File.Exists(filePath))
            //{
            //    using (System.IO.StreamWriter write = new System.IO.StreamWriter(filePath, true))
            //    {
            //        write.Write(etSipServer.ToString());
            //    }
            //}
            var dirPath  = this.FilesDir + "/KnowPool";
            var exists   = Directory.Exists(dirPath);
            var filepath = dirPath + "/cache_data.txt";

            Console.WriteLine("Directory Path : " + filepath);
            if (!exists)
            {
                Directory.CreateDirectory(dirPath);
                if (!System.IO.File.Exists(filepath))
                {
                    var newfile = new Java.IO.File(dirPath, "cache_data.txt");
                    using (Java.IO.FileOutputStream outfile = new Java.IO.FileOutputStream(newfile))
                    {
                        //string line = "The very first line!";
                        outfile.Write(System.Text.Encoding.ASCII.GetBytes(login_result_data));
                        outfile.Flush();
                        outfile.Close();
                    }
                }
            }
            else
            {
                if (!System.IO.File.Exists(filepath))
                {
                    var newfile = new Java.IO.File(dirPath, "cache_data.txt");
                    using (Java.IO.FileOutputStream outfile = new Java.IO.FileOutputStream(newfile))
                    {
                        //string line = "The very first line!";
                        outfile.Write(System.Text.Encoding.ASCII.GetBytes(login_result_data));
                        outfile.Flush();
                        outfile.Close();
                    }
                }
                else
                {
                    using (StreamWriter objStreamWriter = new StreamWriter(filepath, true))
                    {
                        objStreamWriter.WriteLine(login_result_data);
                        objStreamWriter.Flush();
                        objStreamWriter.Close();
                    }
                }
            }
        }
예제 #4
0
        public async Task <string> SaveImageToLibrary(Stream image, string filename)
        {
            var context = contextFactory();

            if (context == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentException(nameof(filename));
            }

            var root      = Android.OS.Environment.ExternalStorageDirectory;
            var imagePath = System.IO.Path.Combine(root.AbsolutePath, filename);
            var directory = System.IO.Path.GetDirectoryName(imagePath);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            var imageFile = new Java.IO.File(imagePath);

            if (!imageFile.Exists())
            {
                imageFile.CreateNewFile();
            }

            using (var file = new Java.IO.FileOutputStream(imageFile))
            {
                var bytes = new byte[image.Length];
                image.Read(bytes, 0, bytes.Length);
                file.Write(bytes, 0, bytes.Length);
                file.Flush();
            }

            image.Seek(0, SeekOrigin.Begin);
            var bitmap = await BitmapFactory.DecodeStreamAsync(image);

            MediaStore.Images.Media.InsertImage(context.ContentResolver, bitmap, imageFile.Name, "");
            context.SendBroadcast(new Intent(Intent.ActionMediaScannerScanFile, Android.Net.Uri.Parse("file://" + root)));

            return(imageFile.AbsolutePath);
        }
예제 #5
0
        public void CacheData(Context context, string key, byte[] data, bool append)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (data == null || data.Length == 0)
            {
                return;
            }

            using (var cacheDir = GetCacheDir(context))
            {
                var size     = cacheDir.TotalSpace - cacheDir.FreeSpace;
                var dataSize = data.Length;
                var newSize  = dataSize + size;

                if (newSize > MaxSizeByte)
                {
                    Clear(cacheDir, newSize - MaxSizeByte);
                }

                if (dataSize > MaxSizeByte)
                {
                    return;
                }

                using (var file = new Java.IO.File(cacheDir, key))
                {
                    using (var stream = new Java.IO.FileOutputStream(file, append))
                    {
                        stream.Write(data);
                        stream.Flush();
                        stream.Close();
                    }
                }
            }
        }
예제 #6
0
        /// <summary>
        /// 外部intentで選択したファイルをアプリ専用ディレクトリ領域に一時フィアルとして保存する。
        /// </summary>
        private Java.IO.File SaveBookShelfTempFile(Intent data)
        {
            //出力ファイルを用意する
            Java.IO.File cacheFile = new Java.IO.File(this.ExternalCacheDir, "bookshelf_cache.txt");
            try
            {
                //入力ファイル
                using (System.IO.Stream fis = this.ContentResolver.OpenInputStream(data.Data))
                {
                    //出力ファイル
                    Java.IO.FileOutputStream fos = new Java.IO.FileOutputStream(cacheFile);

                    byte[] buf = new byte[32768];   // 一時バッファ
                    int    len = 0;
                    while (true)
                    {
                        len = fis.Read(buf, 0, buf.Length);
                        if (len > 0)
                        {
                            fos.Write(buf, 0, len);
                        }
                        else
                        {
                            break;
                        }
                    }
                    //ファイルに書き込む
                    fos.Flush();
                    fos.Close();
                    fis.Close();
                }
                return(cacheFile);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
예제 #7
0
파일: VideoSave.cs 프로젝트: nour7/Ta7meel
        public static void SaveVideoToExternalStorage(string path)
        {
            Java.IO.File root   = Android.OS.Environment.ExternalStorageDirectory;
            Java.IO.File appDir = new Java.IO.File(root, "Ta7meelVideos");
            if (!appDir.Exists())
            {
                if (!appDir.Mkdir())
                {
                    return;
                }
            }
            Java.IO.File videoDownloadedFile = new Java.IO.File(path);
            Java.IO.File file = new Java.IO.File(appDir, videoDownloadedFile.Name);
            if (!file.Exists())
            {
                try
                {
                    Java.IO.FileInputStream  videoToCopy = new Java.IO.FileInputStream(videoDownloadedFile);
                    Java.IO.FileOutputStream copyOfVideo = new Java.IO.FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    int    len;
                    while ((len = videoToCopy.Read(buf)) > 0)
                    {
                        copyOfVideo.Write(buf, 0, len);
                    }

                    copyOfVideo.Flush();
                    copyOfVideo.Close();
                    videoToCopy.Close();
                    String[] Pathes = new String[] { file.Path };
                    MediaScannerConnection.ScanFile(Forms.Context, Pathes, null, null);
                }
                catch (Exception e)
                {
                    e.GetType();
                }
            }
        }
예제 #8
0
        public static void Cache(Context context, byte[] data, String name)
        {
            Java.IO.File cacheDir = context.CacheDir;
            long         size     = GetDirSize(cacheDir);
            long         newSize  = data.Length + size;

            if (newSize > MAX_CACHE_SIZE)
            {
                CleanDir(cacheDir, newSize - MAX_CACHE_SIZE);
            }

            Java.IO.File             file = new Java.IO.File(cacheDir, name);
            Java.IO.FileOutputStream os   = new Java.IO.FileOutputStream(file);
            try
            {
                os.Write(data);
            }
            finally
            {
                os.Flush();
                os.Close();
            }
        }
        public async Task <string> Save(byte[] data, string thefileName)
        {
            string root     = null;
            string fileName = thefileName;

            root = await GetPath();

            root = Path.Combine(root, Android.OS.Environment.DirectoryDownloads);

            Java.IO.File file     = new Java.IO.File(root, fileName);
            string       filePath = file.Path;

            if (file.Exists())
            {
                file.Delete();
            }
            Java.IO.FileOutputStream outs = new Java.IO.FileOutputStream(file);
            outs.Write(data);
            var ab = file.Path;

            outs.Flush();
            outs.Close();
            return(filePath);
        }
예제 #10
0
        public async Task SaveAndView(string fileName, string contentType, MemoryStream stream, PDFOpenContext context)
        {
            string exception = string.Empty;
            string root      = null;

            if (ContextCompat.CheckSelfPermission(Android.App.Application.Context, Manifest.Permission.WriteExternalStorage) != Permission.Granted)
            {
                ActivityCompat.RequestPermissions((Activity)Android.App.Application.Context, new string[] { Manifest.Permission.WriteExternalStorage }, 1);
            }

            //if (Android.OS.Environment.IsExternalStorageEmulated)
            //{
            //    root = Android.OS.Environment.ExternalStorageDirectory.ToString();
            //}
            //else
            //    root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            Java.IO.File myDir = new Java.IO.File(Path.Combine(root, "PDFFiles"));
            myDir.Mkdir();

            Java.IO.File file = new Java.IO.File(myDir, fileName);

            if (file.Exists())
            {
                file.Delete();
            }

            //file.CreateNewFile();

            try
            {
                Java.IO.FileOutputStream outs = new Java.IO.FileOutputStream(file);
                outs.Write(stream.ToArray());

                outs.Flush();
                outs.Close();
            }
            catch (Exception e)
            {
                exception = e.ToString();
            }

            if (file.Exists() && contentType != "application/html")
            {
                string extension = MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
                string mimeType  = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
                Intent intent    = new Intent(Intent.ActionView);
                intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
                Android.Net.Uri path = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
                intent.SetDataAndType(path, mimeType);
                intent.AddFlags(ActivityFlags.GrantReadUriPermission);

                switch (context)
                {
                default:
                case PDFOpenContext.InApp:
                    Android.App.Application.Context.StartActivity(intent);
                    break;

                case PDFOpenContext.ChooseApp:
                    Android.App.Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
                    break;
                }
            }
        }
예제 #11
0
 public override void Flush()
 {
     _fileOutputStream?.Flush();
     //_fileInputStream;
 }