public long GetFileLength(Android.Net.Uri uri) { // returning size long result = -1; if (uri.Scheme.Equals("content")) { try { // Query the source to get File info var cursor = ContentResolver.Query(uri, null, null, null, null); // Get column index of DisplayName int nameIndex = cursor.GetColumnIndex(OpenableColumns.Size); // Move the cursor to the first row cursor.MoveToFirst(); // Get file name from DisplayName column result = cursor.GetLong(nameIndex); // Dispose cursor.Close(); } catch (Exception e) { } } // If first method fails if (result == -1) { var juri = new Java.Net.URI(uri.ToString()); var f = new Java.IO.File(juri); result = f.Length(); } // return name return(result); }
public byte[] imageToByteArray(Java.Net.URI imageIn, byte[] bytes) { Java.IO.File file = new Java.IO.File(imageIn); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; try { for (int readNum; (readNum = fis.Read(buf)) != -1;) { bos.Write(buf, 0, readNum); } } catch (Exception) { throw new Exception(); } bytes = bos.ToByteArray(); return(bytes); }