Exemple #1
0
 public void CreateTextFile(string name, string text)
 {
     try
     {
         IIOContext io   = IOContext.Current;
         string     path = io.TranslateLocalPath(name);
         if (!io.Exists(path))
         {
             string dir = Path.GetDirectoryName(path);
             io.CreateDirectory(dir);
             using (var stream = io.FileStream(path, FileMode.CreateNew))
                 using (var writer = new StreamWriter(stream))
                     writer.Write(text);
         }
         else
         {
             throw new NonFatalException(D.FILE_ALREADY_EXISTS + ": " + name);
         }
     }
     catch (CustomException e)
     {
         throw ClientException(e.FriendlyMessage);
     }
     catch (ArgumentException)
     {
         throw ClientException(D.INVALID_PATH + ": " + name);
     }
     catch (Exception e)
     {
         HandleException(e, "Dir");
         throw ClientException(D.UNEXPECTED_ERROR_OCCURED);
     }
 }
Exemple #2
0
        public bool CreateDirectory(string name)
        {
            try
            {
                IIOContext io   = IOContext.Current;
                string     path = io.TranslateLocalPath(name);

                return(io.CreateDirectory(path));
            }
            catch (CustomException e)
            {
                throw ClientException(e.FriendlyMessage);
            }
            catch (ArgumentException)
            {
                throw ClientException(D.INVALID_PATH + ": " + name);
            }
            catch (Exception e)
            {
                HandleException(e, "CreateDirectory");
                throw ClientException(D.UNEXPECTED_ERROR_OCCURED);
            }
        }
        private bool CopyImageFromGallery(Intent data, string destPath, int size)
        {
            Bitmap bitmap = null;

            try
            {
                string sourcePath;
                if (TryGetPath(data, out sourcePath))
                {
                    if (size == 0)
                    {
                        string dir = Path.GetDirectoryName(destPath);
                        if (dir != null && !Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        File.Copy(sourcePath, destPath);
                        return(true);
                    }
                    bitmap = Helper.LoadBitmap(sourcePath, size, size, false);
                }
                else if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
                {
                    Uri uri = data.Data;
                    Activity.ContentResolver.TakePersistableUriPermission(uri
                                                                          , data.Flags & (ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission));
                    //var stream = Activity.ContentResolver.OpenInputStream(uri);
                    bitmap = Helper.LoadBitmap(() => Activity.ContentResolver.OpenInputStream(uri), size, size);
                }
                else
                {
                    throw new NotImplementedException("Unexpected behavior");
                }


                IIOContext io      = IOContext.Current;
                string     dirPath = Path.GetDirectoryName(destPath);
                if (dirPath != null)
                {
                    io.CreateDirectory(dirPath);
                }

                using (var stream = io.FileStream(destPath, FileMode.Create))
                    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);


                return(true);
            }
            catch (Exception e)
            {
                BitBrowserApp.Current.ExceptionHandler.HandleNonFatal(e);
                return(false);
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Dispose();
                }
            }
        }