/**
             * Create a Bitmap from the URI for that image and return it.
             */
            public async Task <Bitmap> GetBitmapFromUriAsync(Android.Net.Uri uri)
            {
                ParcelFileDescriptor parcelFileDescriptor = null;

                try {
                    parcelFileDescriptor = Activity.ContentResolver.OpenFileDescriptor(uri, "r");
                    var fileDescriptor = parcelFileDescriptor.FileDescriptor;
                    var image          = await BitmapFactory.DecodeFileDescriptorAsync(fileDescriptor).ConfigureAwait(false);

                    parcelFileDescriptor.Close();
                    Log.Info(TAG, "Asynchronous Bitmap Decoding Complete!");
                    return(image);
                } catch (Java.Lang.Exception e) {
                    Log.Error(TAG, "Failed to load image.", e);
                    return(null);
                } finally {
                    try {
                        if (parcelFileDescriptor != null)
                        {
                            parcelFileDescriptor.Close();
                        }
                    } catch (IOException e) {
                        e.PrintStackTrace();
                        Log.Error(TAG, "Error closing ParcelFile Descriptor");
                    }
                }
            }
Exemplo n.º 2
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode != Result.Ok)
            {
                return;
            }

            if (requestCode == PICK_IMAGE)
            {
                GoOnTapApplication.Config.IconSetUri = data.Data;

                // Read icon set
                ParcelFileDescriptor parcelFileDescriptor = ContentResolver.OpenFileDescriptor(data.Data, "r");
                FileDescriptor       fileDescriptor       = parcelFileDescriptor.FileDescriptor;

                FileInputStream fileInputStream = new FileInputStream(fileDescriptor);
                MemoryStream    memoryStream    = new MemoryStream();

                byte[] buffer = new byte[1024];
                int    count;
                while ((count = fileInputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    memoryStream.Write(buffer, 0, count);
                }

                fileInputStream.Close();
                parcelFileDescriptor.Close();

                GoOnTapApplication.Config.IconSetBytes = memoryStream.ToArray();
            }
        }
Exemplo n.º 3
0
        public Bitmap[] GetPages(int pageFrom, int pageTo)
        {
            if (pageFrom == pageTo)
            {
                return(null);
            }
            if (pageFrom > pageTo)
            {
                return(GetPages(pageTo, pageFrom));
            }
            if (pageFrom < 0 || pageFrom >= PageCount || pageTo > PageCount)
            {
                return(null);
            }

            Bitmap[] bitmaps = new Bitmap[pageTo - pageFrom];

            float quality = 2.0F;

            ParcelFileDescriptor parcelFileDescriptor = null;
            PdfRenderer          pdfRenderer          = null;

            try
            {
                parcelFileDescriptor = GetSeekableFileDescriptor();
                pdfRenderer          = GetPdfRenderer(parcelFileDescriptor);

                for (int i = 0; i < bitmaps.Length; ++i)
                {
                    string cacheKey = GetCacheKey(i + pageFrom);
                    Bitmap bitmap   = _pageCache.GetBitmapFromMemCache(new Java.Lang.String(cacheKey));

                    if (bitmap == null)
                    {
                        Page page = pdfRenderer.OpenPage(i + pageFrom);

                        int width  = Java.Lang.Math.Round(page.Width * quality);
                        int height = Java.Lang.Math.Round(page.Height * quality);
                        bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);

                        page.Render(bitmap, null, null, PdfRenderMode.ForDisplay);

                        page.Close();
                        _pageCache.AddBitmapToMemoryCache(new Java.Lang.String(cacheKey), bitmap);
                    }
                    bitmaps[i] = bitmap;
                }
            }
            catch (IOException ex)
            {
                //log
            }
            finally
            {
                parcelFileDescriptor.Close();
                pdfRenderer.Close();
            }
            return(bitmaps);
        }
Exemplo n.º 4
0
        async Task <BitmapFactory.Options> GetBitmapOptionsOfImageAsync(Uri uri)
        {
            BitmapFactory.Options options = new BitmapFactory.Options
            {
                InJustDecodeBounds = true
            };

            // The result will be null because InJustDecodeBounds == true.

            ParcelFileDescriptor parcelFileDescriptor = null;

            try
            {
                parcelFileDescriptor = this.ContentResolver.OpenFileDescriptor(uri, "r");
                var    fileDescriptor = parcelFileDescriptor.FileDescriptor;
                Bitmap result         = await BitmapFactory.DecodeFileDescriptorAsync(fileDescriptor, null, options).ConfigureAwait(false);

                parcelFileDescriptor.Close();
            }
            catch (Java.Lang.Exception e)
            {
                // Log.Error(TAG, "Failed to load image.", e);
                e.PrintStackTrace();
                return(null);
            }
            finally
            {
                try
                {
                    if (parcelFileDescriptor != null)
                    {
                        parcelFileDescriptor.Close();
                    }
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                    // Log.Error(TAG, "Error closing ParcelFile Descriptor");
                }
            }

            int imageHeight = options.OutHeight;
            int imageWidth  = options.OutWidth;

            return(options);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads a <see cref="Bitmap"/> from the provided <see cref="Android.Net.Uri"/> using the provided <see cref="ContentResolver"/>.
        /// WARNING: This method loads the full resolution image and may cause <see cref="Java.Lang.OutOfMemoryError"/>s for images that are too large.
        /// </summary>
        /// <param name="contentResolver">A <see cref="ContentResolver"/> to use to load the image.</param>
        /// <param name="uri">A <see cref="Android.Net.Uri"/> that identifies the image.</param>
        /// <returns></returns>
        public static Bitmap GetBitmapFromUri(ContentResolver contentResolver, AndroidUri uri)
        {
            ParcelFileDescriptor parcelFileDescriptor = contentResolver.OpenFileDescriptor(uri, "r");
            FileDescriptor       fileDescriptor       = parcelFileDescriptor.FileDescriptor;
            Bitmap image = BitmapFactory.DecodeFileDescriptor(fileDescriptor);

            parcelFileDescriptor.Close();
            return(image);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode,
                                                 Intent resultData)
        {
            if (resultCode == Result.Ok)
            {
                // The document selected by the user won't be returned in the intent.
                // Instead, a URI to that document will be contained in the return intent
                // provided to this method as a parameter.
                // Pull that URI using resultData.getData().
                Uri uri = null;
                if (resultData != null)
                {
                    uri = resultData.Data;

                    ParcelFileDescriptor parcelFileDescriptor =
                        ContentResolver.OpenFileDescriptor(uri, "r");
                    FileDescriptor fileDescriptor = parcelFileDescriptor.FileDescriptor;
                    Bitmap         image          = BitmapFactory.DecodeFileDescriptor(fileDescriptor);
                    parcelFileDescriptor.Close();

                    var store = IsolatedStorageFile.GetUserStoreForApplication();
                    if (store.FileExists(Path))
                    {
                        store.DeleteFile(Path);
                    }

                    var          fs     = store.CreateFile(Path);
                    MemoryStream buffer = new MemoryStream();
                    image.Compress(Bitmap.CompressFormat.Png, 100, buffer);
                    buffer.Seek(0, SeekOrigin.Begin);
                    byte[] bytes = new byte[(int)buffer.Length];
                    buffer.Read(bytes, 0, (int)buffer.Length);

                    fs.Write(bytes, 0, bytes.Length);
                    fs.Close();
                    Texture2D text  = null;
                    var       file2 = store.OpenFile(Path, FileMode.Open);

                    text = Texture2D.FromStream(Game1.self.GraphicsDevice, file2);
                    file2.Close();
                    file2.Dispose();

                    Game1.self.config.listOfCards.Single(p => p.Name == Path).SkinPath = Path;
                    Game1.self.ShipsSkins.Single(p => p.ship == Path).skin             = Game1.self.loadTexture2D(Path);
                    var           file   = store.OpenFile("Config_Cards", FileMode.Open);
                    TextWriter    writer = new StreamWriter(file);
                    XmlSerializer xml    = new XmlSerializer(typeof(Cards));
                    xml.Serialize(writer, Game1.self.config);
                    writer.Close();
                    file.Close();
                    store.Close();
                }
            }
        }
        public void CommitWrite()
        {
            ParcelFileDescriptor fileDescriptor = _ctx.ContentResolver.OpenFileDescriptor(Android.Net.Uri.Parse(_path), "w");

            using (var outputStream = new FileOutputStream(fileDescriptor.FileDescriptor))
            {
                byte[] data = _memoryStream.ToArray();
                outputStream.Write(data, 0, data.Length);
                outputStream.Close();
            }
            fileDescriptor.Close();
        }
Exemplo n.º 8
0
        protected override void Dispose(bool disposing)
        {
            if (!disposing || _isDisposed)
            {
                return;
            }
            _isDisposed = true;

            (_stream as Java.IO.ICloseable)?.Close();
            _stream?.Dispose();

            ParcelFileDescriptor?.Close();
            ParcelFileDescriptor?.Dispose();
        }
Exemplo n.º 9
0
 protected void closeSockets()
 {
     if (sPipeApi == PIPE_API_LS)
     {
         try {
             mReceiver.Close();
         } catch (Exception e) {
             System.Diagnostics.Trace.WriteLine(e.StackTrace.ToString());
         }
         try {
             mSender.Close();
         } catch (Exception e) {
             System.Diagnostics.Trace.WriteLine(e.StackTrace.ToString());
         }
         try {
             mLss.Close();
         } catch (Exception e) {
             System.Diagnostics.Trace.WriteLine(e.StackTrace.ToString());
         }
         mLss      = null;
         mSender   = null;
         mReceiver = null;
     }
     else
     {
         try {
             if (mParcelRead != null)
             {
                 mParcelRead.Close();
             }
         } catch (Exception e) {
             System.Diagnostics.Trace.WriteLine(e.StackTrace.ToString());
         }
         try {
             if (mParcelWrite != null)
             {
                 mParcelWrite.Close();
             }
         } catch (Exception e) {
             System.Diagnostics.Trace.WriteLine(e.StackTrace.ToString());
         }
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Loads a <see cref="Bitmap"/> from the provided <see cref="Android.Net.Uri"/> using the provided <see cref="ContentResolver"/>.
        /// </summary>
        /// <param name="contentResolver">A <see cref="ContentResolver"/> to use to load the image.</param>
        /// <param name="uri">A <see cref="Android.Net.Uri"/> that identifies the image.</param>
        /// <param name="reqWidth">The requested width for the loaded <see cref="Bitmap"/>. The actual width after the bitmap is loaded may be larger, but will not be smaller.</param>
        /// <param name="reqHeight">The requested height for the loaded <see cref="Bitmap"/>. The actual height after the bitmap is loaded may be larger, but will not be smaller.</param>
        /// <returns></returns>
        public static Bitmap GetBitmapFromUri(ContentResolver contentResolver, AndroidUri uri, int reqWidth, int reqHeight)
        {
            ParcelFileDescriptor parcelFileDescriptor = contentResolver.OpenFileDescriptor(uri, "r");
            FileDescriptor       fileDescriptor       = parcelFileDescriptor.FileDescriptor;

            // Load just the dimensions of the image (doesn't actually allocate memory for the bitmap)
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InJustDecodeBounds = true;
            BitmapFactory.DecodeFileDescriptor(fileDescriptor, null, options);

            // Calculate inSampleSize so we can subsample the image appropriately.
            options.InSampleSize = CalculateInSampleSize(options.OutWidth, options.OutHeight, reqWidth, reqHeight);

            // Load the image into a Bitmap at the determined subsampling
            options.InJustDecodeBounds = false;
            Bitmap image = BitmapFactory.DecodeFileDescriptor(fileDescriptor, null, options);

            parcelFileDescriptor.Close();
            return(image);
        }
Exemplo n.º 11
0
        private void InitPageCount()
        {
            ParcelFileDescriptor parcelFileDescriptor = null;
            PdfRenderer          pdfRenderer          = null;

            try
            {
                parcelFileDescriptor = GetSeekableFileDescriptor();
                pdfRenderer          = GetPdfRenderer(parcelFileDescriptor);
                PageCount            = pdfRenderer.PageCount;
            }
            catch (IOException e)
            {
                PageCount = 0;
            }
            finally
            {
                parcelFileDescriptor.Close();
                pdfRenderer.Close();
            }
        }
Exemplo n.º 12
0
 public void Close()
 {
     descriptor?.FileDescriptor?.Sync();
     descriptor?.Close();
     descriptor = null;
 }