Last active
November 29, 2024 16:28
-
Star
(160)
You must be signed in to star a gist -
Fork
(51)
You must be signed in to fork a gist
-
-
Save Mariovc/f06e70ebe8ca52fbbbe2 to your computer and use it in GitHub Desktop.
Revisions
-
Mariovc revised this gist
Jul 21, 2016 . 3 changed files with 1 addition and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> -
Mariovc revised this gist
Mar 25, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -76,7 +76,7 @@ public static Bitmap getImageFromResult(Context context, int resultCode, Uri selectedImage; boolean isCamera = (imageReturnedIntent == null || imageReturnedIntent.getData() == null || imageReturnedIntent.getData().toString().contains(imageFile.toString())); if (isCamera) { /** CAMERA **/ selectedImage = Uri.fromFile(imageFile); } else { /** ALBUM **/ -
Mariovc revised this gist
Mar 25, 2016 . 1 changed file with 16 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -173,14 +173,23 @@ private static int getRotationFromCamera(Context context, Uri imageFile) { } public static int getRotationFromGallery(Context context, Uri imageUri) { int result = 0; String[] columns = {MediaStore.Images.Media.ORIENTATION}; Cursor cursor = null; try { cursor = context.getContentResolver().query(imageUri, columns, null, null, null); if (cursor != null && cursor.moveToFirst()) { int orientationColumnIndex = cursor.getColumnIndex(columns[0]); result = cursor.getInt(orientationColumnIndex); } } catch (Exception e) { //Do nothing } finally { if (cursor != null) { cursor.close(); } }//End of try-catch block return result; } -
Mariovc revised this gist
Nov 6, 2015 . 1 changed file with 22 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,22 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.ResolveInfo; import android.content.res.AssetFileDescriptor; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; import android.net.Uri; import android.os.Parcelable; import android.provider.MediaStore; import android.util.Log; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; /** * Author: Mario Velasco Casquero @@ -55,7 +74,9 @@ public static Bitmap getImageFromResult(Context context, int resultCode, File imageFile = getTempFile(context); if (resultCode == Activity.RESULT_OK) { Uri selectedImage; boolean isCamera = (imageReturnedIntent == null || imageReturnedIntent.getData() == null || imageReturnedIntent.getData().equals(Uri.fromFile(imageFile))); if (isCamera) { /** CAMERA **/ selectedImage = Uri.fromFile(imageFile); } else { /** ALBUM **/ -
Mariovc revised this gist
Oct 27, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,7 @@ public static Bitmap getImageFromResult(Context context, int resultCode, File imageFile = getTempFile(context); if (resultCode == Activity.RESULT_OK) { Uri selectedImage; boolean isCamera = (imageReturnedIntent == null || imageReturnedIntent.getData() == null); if (isCamera) { /** CAMERA **/ selectedImage = Uri.fromFile(imageFile); } else { /** ALBUM **/ -
Mariovc revised this gist
Oct 16, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -103,11 +103,11 @@ private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) private static Bitmap getImageResized(Context context, Uri selectedImage) { Bitmap bm = null; int[] sampleSizes = new int[]{5, 3, 2, 1}; int i = 0; do { bm = decodeBitmap(context, selectedImage, sampleSizes[i]); Log.d(TAG, "resizer: new bitmap width = " + bm.getWidth()); i++; } while (bm.getWidth() < minWidthQuality && i < sampleSizes.length); return bm; } -
Mariovc revised this gist
Oct 2, 2015 . 1 changed file with 31 additions and 21 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -58,15 +58,14 @@ public static Bitmap getImageFromResult(Context context, int resultCode, boolean isCamera = imageReturnedIntent == null; if (isCamera) { /** CAMERA **/ selectedImage = Uri.fromFile(imageFile); } else { /** ALBUM **/ selectedImage = imageReturnedIntent.getData(); } Log.d(TAG, "selectedImage: " + selectedImage); bm = getImageResized(context, selectedImage); int rotation = getRotation(context, selectedImage, isCamera); bm = rotate(bm, rotation); } return bm; } @@ -113,21 +112,24 @@ private static Bitmap getImageResized(Context context, Uri selectedImage) { return bm; } private static int getRotation(Context context, Uri imageUri, boolean isCamera) { int rotation; if (isCamera) { rotation = getRotationFromCamera(context, imageUri); } else { rotation = getRotationFromGallery(context, imageUri); } Log.d(TAG, "Image rotation: " + rotation); return rotation; } private static int getRotationFromCamera(Context context, Uri imageFile) { int rotate = 0; try { context.getContentResolver().notifyChange(imageFile, null); ExifInterface exif = new ExifInterface(imageFile.getPath()); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); @@ -143,23 +145,31 @@ private static int getCameraPhotoOrientation(Context context, File imageFile) { rotate = 90; break; } } catch (Exception e) { e.printStackTrace(); } return rotate; } public static int getRotationFromGallery(Context context, Uri imageUri) { String[] columns = {MediaStore.Images.Media.ORIENTATION}; Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null); if (cursor == null) return 0; cursor.moveToFirst(); int orientationColumnIndex = cursor.getColumnIndex(columns[0]); return cursor.getInt(orientationColumnIndex); } private static Bitmap rotate(Bitmap bm, int rotation) { if (rotation != 0) { Matrix matrix = new Matrix(); matrix.postRotate(rotation); Bitmap bmOut = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); return bmOut; } return bm; } } -
Mariovc revised this gist
Oct 1, 2015 . 1 changed file with 11 additions and 29 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ public static Intent getPickImageIntent(Context context) { intentList = addIntentsToList(context, intentList, takePhotoIntent); if (intentList.size() > 0) { chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), context.getString(R.string.pick_image_intent_text)); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{})); } @@ -55,7 +55,8 @@ public static Bitmap getImageFromResult(Context context, int resultCode, File imageFile = getTempFile(context); if (resultCode == Activity.RESULT_OK) { Uri selectedImage; boolean isCamera = imageReturnedIntent == null; if (isCamera) { /** CAMERA **/ selectedImage = Uri.fromFile(imageFile); } else { /** ALBUM **/ selectedImage = imageReturnedIntent.getData(); @@ -97,14 +98,18 @@ private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) return actuallyUsableBitmap; } /** * Resize to avoid using too much memory loading big images (e.g.: 2560*1920) **/ private static Bitmap getImageResized(Context context, Uri selectedImage) { Bitmap bm = null; int[] sampleSizes = new int[]{5, 3, 2, 1}; int i = -1; do { i++; bm = decodeBitmap(context, selectedImage, sampleSizes[i]); Log.d(TAG, "resizer: new bitmap width = " + bm.getWidth()); } while (bm.getWidth() < minWidthQuality && i < sampleSizes.length); return bm; } @@ -113,32 +118,9 @@ private static Bitmap rotateIfNeeded(Context context, Bitmap bm, File imageFile) if (rot != 0) { bm = new RotateOrientation().RotateOrientationCall(bm, rot); } return bm; } private static int getCameraPhotoOrientation(Context context, File imageFile) { int rotate = 0; -
Mariovc revised this gist
Sep 9, 2015 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -63,7 +63,9 @@ public static Bitmap getImageFromResult(Context context, int resultCode, Log.d(TAG, "selectedImage: " + selectedImage); bm = getImageResized(context, selectedImage); if (isCamera) { bm = rotateIfNeeded(context, bm, imageFile); } } return bm; } -
Mariovc renamed this gist
Sep 8, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ public class SimpleActivity { private static final int PICK_IMAGE_ID = 234; // the number doesn't matter public void onPickImage(View view) { -
Mariovc revised this gist
Sep 8, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ public class ExampleActivity { private static final int PICK_IMAGE_ID = 234; // the number doesn't matter public void onPickImage(View view) { Intent chooseImageIntent = ImagePicker.getPickImageIntent(this); -
Mariovc revised this gist
Sep 8, 2015 . 1 changed file with 21 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ public class RegisterActivity { private static final int PICK_IMAGE_ID = 234; public void onPickImage(View view) { Intent chooseImageIntent = ImagePicker.getPickImageIntent(this); startActivityForResult(chooseImageIntent, PICK_IMAGE_ID); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case PICK_IMAGE_ID: Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data); // TODO use bitmap break; default: super.onActivityResult(requestCode, resultCode, data); break; } } } -
Mariovc created this gist
Sep 8, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,181 @@ /** * Author: Mario Velasco Casquero * Date: 08/09/2015 * Email: [email protected] */ public class ImagePicker { private static final int DEFAULT_MIN_WIDTH_QUALITY = 400; // min pixels private static final String TAG = "ImagePicker"; private static final String TEMP_IMAGE_NAME = "tempImage"; public static int minWidthQuality = DEFAULT_MIN_WIDTH_QUALITY; public static Intent getPickImageIntent(Context context) { Intent chooserIntent = null; List<Intent> intentList = new ArrayList<>(); Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePhotoIntent.putExtra("return-data", true); takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context))); intentList = addIntentsToList(context, intentList, pickIntent); intentList = addIntentsToList(context, intentList, takePhotoIntent); if (intentList.size() > 0) { chooserIntent = Intent.createChooser(intentList.remove(intentList.size() -1), context.getString(R.string.pick_image_intent_text)); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{})); } return chooserIntent; } private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) { List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0); for (ResolveInfo resolveInfo : resInfo) { String packageName = resolveInfo.activityInfo.packageName; Intent targetedIntent = new Intent(intent); targetedIntent.setPackage(packageName); list.add(targetedIntent); Log.d(TAG, "Intent: " + intent.getAction() + " package: " + packageName); } return list; } public static Bitmap getImageFromResult(Context context, int resultCode, Intent imageReturnedIntent) { Log.d(TAG, "getImageFromResult, resultCode: " + resultCode); Bitmap bm = null; File imageFile = getTempFile(context); if (resultCode == Activity.RESULT_OK) { Uri selectedImage; if (imageReturnedIntent == null) { /** CAMERA **/ selectedImage = Uri.fromFile(imageFile); } else { /** ALBUM **/ selectedImage = imageReturnedIntent.getData(); } Log.d(TAG, "selectedImage: " + selectedImage); bm = getImageResized(context, selectedImage); bm = rotateIfNeeded(context, bm, imageFile); } return bm; } private static File getTempFile(Context context) { File imageFile = new File(context.getExternalCacheDir(), TEMP_IMAGE_NAME); imageFile.getParentFile().mkdirs(); return imageFile; } private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; AssetFileDescriptor fileDescriptor = null; try { fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r"); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor( fileDescriptor.getFileDescriptor(), null, options); Log.d(TAG, options.inSampleSize + " sample method bitmap ... " + actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight()); return actuallyUsableBitmap; } private static Bitmap getImageResized(Context context, Uri selectedImage) { Bitmap bm = null; int sampleSize = 7; // TODO improve resizer do { sampleSize -= 2; bm = decodeBitmap(context, selectedImage, sampleSize); Log.d(TAG, "resizer: new bitmap width = " + bm.getWidth()); } while (bm.getWidth() < minWidthQuality && sampleSize > 1); return bm; } private static Bitmap rotateIfNeeded(Context context, Bitmap bm, File imageFile) { int rot = getCameraPhotoOrientation(context, imageFile); if (rot != 0) { bm = new RotateOrientation().RotateOrientationCall(bm, rot); } // saveBitmap(imageFile, bm); return bm; } // private static void saveBitmap(File imageFile, Bitmap bm) { // Log.d(TAG, "Saving image ... "); // imageFile.getParentFile().mkdirs(); // FileOutputStream out = null; // try { // out = new FileOutputStream(imageFile); // bm.compress(Bitmap.CompressFormat.PNG, 90, out); // } catch (Exception e) { // e.printStackTrace(); // } finally { // try { // if (out != null) { // out.close(); // } // } catch (IOException e) { // e.printStackTrace(); // } // } // Log.d(TAG, "Image saved"); // } private static int getCameraPhotoOrientation(Context context, File imageFile) { int rotate = 0; try { context.getContentResolver().notifyChange(Uri.fromFile(imageFile), null); ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; } Log.d(TAG, "Exit orientation: " + orientation); } catch (Exception e) { e.printStackTrace(); } return rotate; } private static class RotateOrientation { Bitmap RotateOrientationCall(Bitmap src, float degree) { Matrix matrix = new Matrix(); matrix.postRotate(degree); Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); return bmOut; } } }