public static string SaveBitmapGalerry(Bitmap bitmap) { CreateDirectoryForPictures(); ContextWrapper cw = new ContextWrapper(Application.Context); Java.IO.File directory = cw.GetDir("LegionApp", FileCreationMode.Private); DateTime dt = DateTime.Now; string filename = (dt.ToString("yyyyMMddHHmmss") + "_img.png"); Java.IO.File myPath = new Java.IO.File(_dir, filename); var stream = new FileStream(myPath.AbsolutePath, FileMode.Create); bitmap.Compress(Bitmap.CompressFormat.Png, 50, stream); stream.Flush(); stream.Close(); bitmap.Recycle(); bitmap = null; MediaStore.Images.Media.InsertImage(cw.ContentResolver, myPath.AbsolutePath, filename, filename); return(filename); }
//ver foto protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) { try { base.OnActivityResult(requestCode, resultCode, data); Bitmap bitmap = (Bitmap)data.Extras.Get("data"); perfil.SetImageBitmap(bitmap); ContextWrapper cw = new ContextWrapper(this.ApplicationContext); var fileName = cw.GetDir("imgDir", FileCreationMode.Private).ToString() + "/profile.png"; ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); ISharedPreferencesEditor editor = prefs.Edit(); editor.PutString("Profile_picture", fileName.ToString()); editor.Apply(); try { var os1 = new FileStream(fileName, FileMode.Truncate); os1.Close(); } catch (Exception ex) { int x = 0; } using (var os = new FileStream(fileName, FileMode.OpenOrCreate)) { bitmap.Compress(Bitmap.CompressFormat.Jpeg, 95, os); os.Close(); } } catch (Exception e) { } }
public async Task <IReadOnlyCollection <string> > GetFilesAsync(string directoryPath) { var cw = new ContextWrapper(Application.Context); var directory = cw.GetDir(directoryPath, FileCreationMode.Private); var files = await directory.ListFilesAsync(); return(files.Select(f => f.AbsolutePath).ToReadOnlyCollection()); }
//This generates something like = /data/data/yourapp/app_data/imageDir/123_user_image.png public static string GetUserProfileImagePath(string id) { using (var cw = new ContextWrapper(Application.Context)) { // path to /data/data/yourapp/app_data/imageDir var directory = cw.GetDir("imageDir", FileCreationMode.Private); return(Path.Combine(directory.Path, BuildImageFilename(id))); } }
public static async Task RemoveFile(string filename) { try { using (var cw = new ContextWrapper(Application.Context)) { // path to /data/data/yourapp/app_data/imageDir var directory = cw.GetDir("imageDir", FileCreationMode.Private); string fullPath = Path.Combine(directory.Path, filename); File.Delete(fullPath); } } catch (Exception) { //no file } await Task.Delay(1000); }
private string SaveToInternalStorage(Bitmap bitmap) { ContextWrapper cw = new ContextWrapper(ApplicationContext); File directory = cw.GetDir("pictures", FileCreationMode.Private); File myPath = new File(directory, "picture.png"); try { using (var os = new System.IO.FileStream(myPath.AbsolutePath, System.IO.FileMode.Create)) { bitmap.Compress(Bitmap.CompressFormat.Png, 100, os); os.Close(); } } catch (Exception e) { System.Diagnostics.Debug.Write("Deze lijn: " + e.Message); } return(directory.AbsolutePath); }
public static async Task SaveFile(string filename, byte[] fileBytes) { try { using (var cw = new ContextWrapper(Application.Context)) { // path to /data/data/yourapp/app_data/imageDir var directory = cw.GetDir("imageDir", FileCreationMode.Private); // Create imageDir // var mypath = new File(directory, $"{filename}.jpg"); string fullPath = Path.Combine(directory.Path, filename); await File.WriteAllBytesAsync(fullPath, fileBytes); } } catch (Exception) { //Could save the file } await Task.Delay(1000); }
protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById<Button> (Resource.Id.myButton); button.Click += delegate { //Get image bitmap from url Bitmap imageBitmap = GetImageBitmapFromUrl("http://xamarin.com/content/images/pages/platform/sketches-screenshot.png"); ImageView image = FindViewById<ImageView>(Resource.Id.imageView1); //Save Image to internal storage ContextWrapper cw = new ContextWrapper(this.ApplicationContext); File directory = cw.GetDir("imgDir", FileCreationMode.Private); File myPath = new File(directory, "test.png"); try { using (var os = new System.IO.FileStream(myPath.AbsolutePath, System.IO.FileMode.Create)) { imageBitmap.Compress(Bitmap.CompressFormat.Png, 100, os); } } catch (Exception ex) { System.Console.Write(ex.Message); } //Load Image from internal storage Bitmap loadedImage = BitmapFactory.DecodeFile(myPath.AbsoluteFile.AbsolutePath); image.SetImageBitmap(loadedImage); }; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById <Button> (Resource.Id.myButton); button.Click += delegate { //Get image bitmap from url Bitmap imageBitmap = GetImageBitmapFromUrl("http://xamarin.com/content/images/pages/platform/sketches-screenshot.png"); ImageView image = FindViewById <ImageView>(Resource.Id.imageView1); //Save Image to internal storage ContextWrapper cw = new ContextWrapper(this.ApplicationContext); File directory = cw.GetDir("imgDir", FileCreationMode.Private); File myPath = new File(directory, "test.png"); try { using (var os = new System.IO.FileStream(myPath.AbsolutePath, System.IO.FileMode.Create)) { imageBitmap.Compress(Bitmap.CompressFormat.Png, 100, os); } } catch (Exception ex) { System.Console.Write(ex.Message); } //Load Image from internal storage Bitmap loadedImage = BitmapFactory.DecodeFile(myPath.AbsoluteFile.AbsolutePath); image.SetImageBitmap(loadedImage); }; }
private async Task<Bitmap> GetImageFromUrl(string url) { // let's check if the file exists... ContextWrapper cw = new ContextWrapper (this.ApplicationContext); File directory = cw.GetDir("imgDir", FileCreationMode.Private); string[] elements = url.Split (new char[] { '/' }); bool fileExists = false; string fileName = directory + "/" + elements [elements.Length - 1]; if (System.IO.File.Exists (fileName)) { fileExists = true; var imageFile = new Java.IO.File(fileName); Bitmap bitmap = BitmapFactory.DecodeFile(imageFile.AbsolutePath); return bitmap; } if (!fileExists) { using (var client = new HttpClient ()) { var msg = await client.GetAsync (url); if (msg.IsSuccessStatusCode) { using (var stream = await msg.Content.ReadAsStreamAsync ()) { var bitmap = await BitmapFactory.DecodeStreamAsync (stream); File myPath = new File (directory, elements [elements.Length - 1]); try { using (var os = new System.IO.FileStream (myPath.AbsolutePath, System.IO.FileMode.Create)) { bitmap.Compress (Bitmap.CompressFormat.Png, 100, os); } } catch (Exception ex) { System.Console.Write (ex.Message); } return bitmap; } } } } return null; }
public string upload(byte[] image) { if (AppGlobalData.context == null) return null; ContextWrapper cw = new ContextWrapper(AppGlobalData.context); Java.IO.File directory = cw.GetDir("imgDir", FileCreationMode.WorldReadable); Java.IO.File myPath = new Java.IO.File(directory, "tmpimgfile"); FileStream fs = new FileStream(myPath.AbsolutePath, FileMode.OpenOrCreate); //fs.Write(image, 0, image.Length); string ret = UploadFilesToRemoteUrl("http://joi-testp.chinacloudapp.cn:8090/image/upload/jinmin", fs); fs.Close(); return ret; }
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); // Make it available in the gallery Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); Android.Net.Uri contentUri = Android.Net.Uri.FromFile(App._file); mediaScanIntent.SetData(contentUri); SendBroadcast(mediaScanIntent); // Display in ImageView. We will resize the bitmap to fit the display. // Loading the full sized image will consume to much memory // and cause the application to crash. int height = Resources.DisplayMetrics.HeightPixels; int width = _imageView.Height; App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height); // Save the bitmap to a file ContextWrapper cw = new ContextWrapper(this.ApplicationContext); Java.IO.File directory = cw.GetDir("imgDir", FileCreationMode.Private); Java.IO.File myPath = new Java.IO.File(directory, this.imgFileName); try { using (var os = new System.IO.FileStream(myPath.AbsolutePath, System.IO.FileMode.Create)) { Matrix matrix = new Matrix(); matrix.SetRotate(90); Bitmap scaledBitmap = Bitmap.CreateScaledBitmap(App.bitmap, App.bitmap.Height, App.bitmap.Width/2, true); Bitmap rotatedBitmap = Bitmap.CreateBitmap(scaledBitmap, 0, 0, scaledBitmap.Width, scaledBitmap.Height, matrix, true); App.bitmap = rotatedBitmap; App.bitmap.Compress(Bitmap.CompressFormat.Png, 100, os); os.Close(); } } catch (Exception ex) { System.Console.Write(ex.Message); } // Dispose of the Java side bitmap. GC.Collect(); try { System.IO.FileStream imgFile = new FileStream(myPath.AbsolutePath, FileMode.Open); int b = imgFile.ReadByte(); imgFile.Close(); } catch (Java.IO.IOException e) { } if (App.bitmap != null) { _imageView.SetImageBitmap(App.bitmap); App.bitmap = null; } }
private void UploadImg(object sender, EventArgs eventArgs) { ContextWrapper cw = new ContextWrapper(this.ApplicationContext); Java.IO.File directory = cw.GetDir("imgDir", FileCreationMode.WorldReadable); Java.IO.File myPath = new Java.IO.File(directory, this.imgFileName); System.IO.FileStream imgFile = new FileStream(myPath.AbsolutePath, FileMode.Open); /* BinaryReader br = new BinaryReader(imgFile); long numBytes = new FileInfo(this.imgFileName).Length; byte[] buff = null; buff = br.ReadBytes((int) numBytes); */ DialogService.ShowLoading("Uploading"); try { RESTService.UploadFilesToRemoteUrl("http://joi-testp.chinacloudapp.cn:8090/image/upload/jinmin", imgFile); } catch (WebException e) { } DialogService.HideLoading(); }