public static byte[] PathToByte2(string path){
			//byte[] pathData=null;
			Log.Debug ("ImageToByte", "Inicia path to byte");
			Log.Debug ("ImageToByte", "Se crea el nuevo file");
			var imgFile = new Java.IO.File(path);
			Log.Debug ("ImageToByte", "Se crea el nuevo stream");
			var stream = new Java.IO.FileInputStream(imgFile);
			Log.Debug ("ImageToByte", "Se Crea el archivo Byte");
			var bytes = new byte[imgFile.Length()];
			Log.Debug ("ImageToByte", "Se hace el Stream Read");
			stream.Read(bytes);
			stream.Close ();
			stream.Dispose ();
			return bytes;

		}
Exemplo n.º 2
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok)
            {
                byte[] bytes;

                try
                {
                    using (var stream = new Java.IO.FileInputStream(arquivoImagem)) {
                        bytes = new byte[arquivoImagem.Length()];
                        stream.Read(bytes);
                    }
                } catch
                {
                    Bitmap bitmap = (Bitmap)data.Extras.Get("data");
                    bytes = new byte[bitmap.Width * bitmap.Height * 4];
                    using (var stream = new MemoryStream()) {
                        bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream);
                        stream.Flush();
                    }
                }

                MessagingCenter.Send <byte[]>(bytes, "FotoTirada");
            }
        }
Exemplo n.º 3
0
        private void ExtractVideoPickedFromGallery(Intent data)
        {
            try {
                Android.Net.Uri selectedUri = data.Data;

                string mimeType = "video";

                PickedMediaFromGallery result = new PickedMediaFromGallery();

                if (mimeType.StartsWith("video"))
                {
                    string filePath = MediaUtils.GetFileFullPathAlternativeVideo(data.Data, this);

                    Java.IO.File videoFile = new Java.IO.File(filePath);

                    Java.IO.FileInputStream videoFileInputStream = new Java.IO.FileInputStream(videoFile);
                    byte[] videoFileBytes = new byte[videoFile.Length()];
                    videoFileInputStream.Read(videoFileBytes);
                    videoFileInputStream.Close();
                    videoFileInputStream.Dispose();

                    System.IO.Stream thumbnailStream = DependencyService.Get <IPickMediaDependencyService>().GetThumbnail(filePath);
                    byte[]           thumbnailBytes;

                    thumbnailStream.Position = 0;

                    using (System.IO.BinaryReader reader = new System.IO.BinaryReader(thumbnailStream)) {
                        thumbnailBytes = reader.ReadBytes((int)thumbnailStream.Length);
                    }

                    result = new PickedMediaFromGallery()
                    {
                        Completion          = true,
                        DataBase64          = Android.Util.Base64.EncodeToString(videoFileBytes, Android.Util.Base64Flags.Default),
                        DataThumbnailBase64 = Convert.ToBase64String(thumbnailBytes),
                        FilePath            = filePath,
                        MimeType            = ProfileMediaService.VIDEO_MEDIA_TYPE
                    };
                }
                else
                {
                    Debugger.Break();
                    throw new InvalidOperationException();
                }

                PickVideoTaskCompletion.SetResult(result);
            }
            catch (Exception exc) {
                Debugger.Break();

                PickVideoTaskCompletion.SetResult(new PickedMediaFromGallery()
                {
                    Exception = exc, ErrorMessage = _EXTRACTION_MEDIA_MEDIA_PICK_ERROR
                });
            }
        }
Exemplo n.º 4
0
 protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
 {
     base.OnActivityResult(requestCode, resultCode, data);
     if (resultCode == Result.Ok)
     {
         byte[] bytes;
         using (var stream = new Java.IO.FileInputStream(arquivoImagem))
         {
             bytes = new byte[arquivoImagem.Length()];
             stream.Read(bytes);
         }
         MessagingCenter.Send <byte[]>(bytes, "TirarFoto");
     }
 }
Exemplo n.º 5
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (resultCode == Result.Ok)
            {
                byte[] bytes;

                using (var stream = new Java.IO.FileInputStream(FileImagePath))
                {
                    bytes = new byte[FileImagePath.Length()];
                    stream.Read(bytes);
                }
                MessagingCenter.Send <byte[]>(bytes, "ProfilePicture");
            }
        }
Exemplo n.º 6
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok)
            {
                byte[] bytes;
                using (var stream = new Java.IO.FileInputStream(foto))
                {
                    bytes = new byte[foto.Length()];
                    stream.Read(bytes);
                }
                MessagingCenter.Send(bytes, "FotoTirada");
            }
        }
        //Tratando o resultado da activity, caira nesse trecho mesmo se o usuario aceitar ou nao
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok)   //somente se o usuario nao cancelar a foto

            //Como vamos passar a imagem para o projeto portable precisamos transformar em um array de bytes
            {
                byte[] bytes;
                using (var stream = new Java.IO.FileInputStream(arquivoImagem))
                {
                    bytes = new byte[arquivoImagem.Length()];
                    stream.Read(bytes);
                }

                MessagingCenter.Send <byte[]>(bytes, "FotoTirada");
            }
        }
Exemplo n.º 8
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok)
            {
                //Alterado para enviar array de bytes para testes
                //MessagingCenter.Send<Java.IO.File>(arquivoImagem, "Foto");

                byte[] bytes;
                using (var stream = new Java.IO.FileInputStream(arquivoImagem))
                {
                    bytes = new byte[arquivoImagem.Length()];
                    stream.Read(bytes);
                };

                MessagingCenter.Send <byte[]>(bytes, "Foto");
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            try
            {
                var l = _fileInputStream.Read(buffer, offset, count);
                _totalReaded += l;

                if (l < 0)
                {
                    _progressAction?.Invoke(100);
                    return(0);
                }
                _progressAction?.Invoke((int)(_totalReaded * 100 / Length));
                return(l);
            }
            catch (Exception ex)
            {
                var t = ex;
            }
            return(0);
        }
Exemplo n.º 10
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            try
            {
                var l = _fileInputStream.Read(buffer, offset, count);
                _totalReaded += l;

                if (l < 0)
                {
                    _progressAction?.Invoke(100);
                    return(0);
                }
                _progressAction?.Invoke((int)(_totalReaded * 100 / Length));
                return(l);
            }
            catch (System.Exception ex)
            {
                AppSettings.Logger.Warning(ex);
            }
            return(0);
        }
Exemplo n.º 11
0
        private void ExtractVideoTakedFromCamera(Intent data)
        {
            try {
                Android.Net.Uri selectedUri = data.Data;

                string filePath = MediaUtils.GetFileFullPathAlternativeVideo(data.Data, this);

                Java.IO.File videoFile = new Java.IO.File(filePath);

                Java.IO.FileInputStream videoFileInputStream = new Java.IO.FileInputStream(videoFile);
                byte[] videoFileBytes = new byte[videoFile.Length()];
                videoFileInputStream.Read(videoFileBytes);
                videoFileInputStream.Close();
                videoFileInputStream.Dispose();

                System.IO.Stream thumbnailStream = DependencyService.Get <IPickMediaDependencyService>().GetThumbnail(filePath);
                byte[]           thumbnailBytes;

                thumbnailStream.Position = 0;

                using (System.IO.BinaryReader reader = new System.IO.BinaryReader(thumbnailStream)) {
                    thumbnailBytes = reader.ReadBytes((int)thumbnailStream.Length);
                }

                CompleteActionTakeVideoOrImage(new PickedMediaFromGallery()
                {
                    Completion          = true,
                    DataBase64          = Android.Util.Base64.EncodeToString(videoFileBytes, Android.Util.Base64Flags.Default),
                    DataThumbnailBase64 = Convert.ToBase64String(thumbnailBytes),
                    FilePath            = filePath,
                    MimeType            = ProfileMediaService.VIDEO_MEDIA_TYPE
                });
            }
            catch (Exception exc) {
                CompleteActionTakeVideoOrImage(new PickedMediaFromGallery()
                {
                    Exception = exc, ErrorMessage = _TAKE_VIDEO_OR_IMAGE_TAKED_VIDEO_HANDLING_ERROR
                });
            }
        }
Exemplo n.º 12
0
        public static byte[] Retrieve(Context context, String name)
        {
            Java.IO.File cacheDir = context.CacheDir;
            Java.IO.File file     = new Java.IO.File(cacheDir, name);

            if (!file.Exists())
            {
                return(null);
            }

            byte[] data = new byte[(int)file.Length()];
            Java.IO.FileInputStream stream = new Java.IO.FileInputStream(file);
            try
            {
                stream.Read(data);
            }
            finally
            {
                stream.Close();
            }
            return(data);
        }
Exemplo n.º 13
0
        public byte[] RetrieveData(Context context, string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            using (var file = FindFile(context, key))
            {
                if (file?.Exists() != true)
                {
                    return(null);
                }

                var data = new byte[(int)file.Length()];
                using (var stream = new Java.IO.FileInputStream(file))
                {
                    stream.Read(data);
                    return(data);
                }
            }
        }
Exemplo n.º 14
0
        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();
                }
            }
        }
Exemplo n.º 15
0
        private void ExtractMediaPickedFromGallery(Intent data)
        {
            try {
                Android.Net.Uri selectedUri = data.Data;
                String[]        columns     = { MediaStore.Images.Media.InterfaceConsts.Data,
                                                MediaStore.Images.Media.InterfaceConsts.MimeType };

                ICursor cursor = ContentResolver.Query(selectedUri, columns, null, null, null);
                cursor.MoveToFirst();

                int pathColumnIndex     = cursor.GetColumnIndex(columns[0]);
                int mimeTypeColumnIndex = cursor.GetColumnIndex(columns[1]);

                string mimeType = cursor.GetString(mimeTypeColumnIndex);
                cursor.Close();

                PickedMediaFromGallery result = null;

                if (mimeType.StartsWith("image"))
                {
                    Android.Graphics.Bitmap bitmap = MediaUtils.GetScaledBitmap(
                        data.Data,
                        MediaUtils.IMAGE_WIDTH_RESTRICTION,
                        MediaUtils.IMAGE_HEIGHT_RESTRICTION,
                        this);

                    if (bitmap == null)
                    {
                        bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, data.Data);
                    }

                    byte[] bytes;

                    using (MemoryStream memoryStream = new MemoryStream()) {
                        bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, memoryStream);
                        bytes = memoryStream.GetBuffer();
                    }

                    result = new PickedMediaFromGallery()
                    {
                        DataBase64          = Android.Util.Base64.EncodeToString(bytes, Android.Util.Base64Flags.Default),
                        Completion          = true,
                        DataThumbnailBase64 = Android.Util.Base64.EncodeToString(bytes, Android.Util.Base64Flags.Default),
                        MimeType            = ProfileMediaService.IMAGE_MEDIA_TYPE
                    };
                }
                else if (mimeType.StartsWith("video"))
                {
                    string filePath = MediaUtils.GetFileFullPathAlternativeVideo(data.Data, this);

                    Java.IO.File videoFile = new Java.IO.File(filePath);

                    Java.IO.FileInputStream videoFileInputStream = new Java.IO.FileInputStream(videoFile);
                    byte[] videoFileBytes = new byte[videoFile.Length()];
                    videoFileInputStream.Read(videoFileBytes);
                    videoFileInputStream.Close();
                    videoFileInputStream.Dispose();

                    System.IO.Stream thumbnailStream = DependencyService.Get <IPickMediaDependencyService>().GetThumbnail(filePath);
                    byte[]           thumbnailBytes;

                    thumbnailStream.Position = 0;

                    using (System.IO.BinaryReader reader = new System.IO.BinaryReader(thumbnailStream)) {
                        thumbnailBytes = reader.ReadBytes((int)thumbnailStream.Length);
                    }

                    result = new PickedMediaFromGallery()
                    {
                        Completion          = true,
                        DataBase64          = Android.Util.Base64.EncodeToString(videoFileBytes, Android.Util.Base64Flags.Default),
                        DataThumbnailBase64 = Convert.ToBase64String(thumbnailBytes),
                        FilePath            = filePath,
                        MimeType            = ProfileMediaService.VIDEO_MEDIA_TYPE
                    };
                }
                else
                {
                    Debugger.Break();
                    throw new InvalidOperationException();
                }

                PickMediaTaskCompletion.SetResult(result);
            }
            catch (Exception exc) {
                Debugger.Break();

                PickMediaTaskCompletion.SetResult(new PickedMediaFromGallery()
                {
                    Exception = exc, ErrorMessage = _EXTRACTION_MEDIA_MEDIA_PICK_ERROR
                });
            }
        }