Created
November 20, 2014 22:11
-
-
Save RedTahr/4fa904b1608ab2f932fa to your computer and use it in GitHub Desktop.
Android helper functions in Xamarin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "return" "version" "name" - names aren't syntactically correct (they're probably upper case for starters) | |
// 21 5.0 lollipop | |
// 20 4.4.2W kitkat wear | |
// 19 4.4.2 kitkat | |
// 18 4.3.1 jelly bean mr2 | |
// 17 4.2.2 jelly bean mr1 | |
// 16 4.1.2 jelly bean | |
// 15 4.0.3 ice-cream sandwich mr1 | |
// 14 4.0 ice-cream sandwich | |
// 13 3.2 honeycomb mr2 | |
// 12 3.1 honeycomb mr1 | |
// 10 3.0 honeycomb | |
// 8 2.2 froyo | |
// 7 2.1 eclair | |
// 4 1.6 donut | |
// 3 1.5 cupcake | |
public static string GetAndroidVersion() { | |
return global::Android.OS.Build.VERSION.Sdk; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/6908604/android-crop-center-of-bitmap | |
Bitmap newBitmap = ThumbnailUtils.ExtractThumbnail(currentBitmap, dimensionalConstraintW, dimensionalConstraintH); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static File CreateDirectory(string directory) { | |
File newDir = new File(directory); // yeah android file vs dir terminology | |
if(!newDir.Exists()) { | |
newDir.Mkdir(); | |
} | |
return newDir; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://developer.android.com/guide/topics/data/data-storage.html | |
/* Checks if external storage is available for read and write */ | |
public static bool IsExternalStorageWritable { | |
get { | |
string state = global::Android.OS.Environment.ExternalStorageState; | |
if(global::Android.OS.Environment.MediaMounted.Equals(state)) { | |
return true; | |
} | |
return false; | |
} | |
} | |
/* Checks if external storage is available to at least read */ | |
public static bool IsExternalStorageReadable { | |
get { | |
string state = global::Android.OS.Environment.ExternalStorageState; | |
if(global::Android.OS.Environment.MediaMounted.Equals(state) || | |
global::Android.OS.Environment.MediaMountedReadOnly.Equals(state)) { | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static File GetPicturesFolder(global::Android.App.Activity callingActivity) { | |
string appPicturePath = callingActivity.GetExternalFilesDir(global::Android.OS.Environment.DirectoryPictures).AbsolutePath; | |
return CreateDirectoryForPictures(appPicturePath); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://forums.xamarin.com/discussion/5409/photo-being-saved-in-landscape-not-portrait | |
// resizes the image density, width/height aren't going to be the end dimensions of the image | |
public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height) { | |
// First we get the the dimensions of the file on disk | |
BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true }; | |
BitmapFactory.DecodeFile(fileName, options); | |
// Next we calculate the ratio that we need to resize the image by | |
// in order to fit the requested dimensions. | |
int outHeight = options.OutHeight; | |
int outWidth = options.OutWidth; | |
int inSampleSize = 1; | |
if(outHeight > height || outWidth > width) { | |
inSampleSize = outWidth > outHeight | |
? outHeight / height | |
: outWidth / width; | |
} | |
// Now we will load the image and have BitmapFactory resize it for us. | |
options.InSampleSize = inSampleSize; | |
options.InJustDecodeBounds = false; | |
Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options); | |
// Images are being saved in landscape, so rotate them back to portrait if they were taken in portrait | |
Matrix mtx = new Matrix(); | |
ExifInterface exif = new ExifInterface(fileName); | |
string orientation = exif.GetAttribute(ExifInterface.TagOrientation); | |
//http://developer.android.com/reference/android/media/ExifInterface.html | |
switch(orientation) { | |
case "0": // ORIENTATION_UNDEFINED - Nexus 7 landscape... | |
break; | |
case "1": // landscape - ORIENTATION_NORMAL | |
break; | |
case "2": // ORIENTATION_FLIP_HORIZONTAL | |
mtx.PreScale(-1, 1); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
break; | |
case "3": // ORIENTATION_ROTATE_180 | |
mtx.PreRotate(180); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
case "4": // ORIENTATION_FLIP_VERTICAL | |
mtx.PreScale(1, -1); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
break; | |
case "5": //ORIENTATION_TRANSPOSE | |
break; | |
case "6": // portrait - ORIENTATION_ROTATE_90 | |
mtx.PreRotate(90); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
case "7": // ORIENTATION_TRANSVERSE | |
break; | |
case "8": // ORIENTATION_ROTATE_270 - might need to flip horizontally too... | |
mtx.PreRotate(270); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
default: | |
mtx.PreRotate(90); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
} | |
return resizedBitmap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment