Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. package com.pubtap.android.helpers;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Matrix;
  10. import android.media.ExifInterface;
  11. import android.net.Uri;
  12. import android.os.Environment;
  13. import android.os.Parcel;
  14. import android.os.Parcelable;
  15. import android.provider.MediaStore;
  16. import android.text.TextUtils;
  17. import android.util.Pair;
  18.  
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Date;
  26.  
  27. /**
  28. * Created by Alexandr Timoshenko on 11/15/13.
  29. */
  30. public final class PhotoPickerHelper implements Parcelable {
  31. private Uri mContactPickerUri;
  32.  
  33. public Intent getGalleryIntent(Context context){
  34. Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT, null);
  35. galleryintent.setType("image/*");
  36. return galleryintent;
  37. }
  38. public Pair<Intent,Uri> getCameraIntent(Context context,String bucketName) throws IOException {
  39. Intent cameraIntent = new Intent(
  40. android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  41. cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 2);
  42. Uri fileUri = createImageMediaFile(context,bucketName);
  43. cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  44. cameraIntent.putExtra("return-data", false);
  45. mContactPickerUri = fileUri;
  46. return new Pair<Intent, Uri>(cameraIntent,fileUri);
  47. }
  48.  
  49. public Uri handleCameraResult(Context context,final Intent data) {
  50. Uri contactImageUri = mContactPickerUri;
  51. if (data != null && data.getData() != null) {
  52. contactImageUri = data.getData();
  53. } else {
  54. rotateFileAndReplace(contactImageUri.toString());
  55. }
  56. String imageUri = getPath(context, contactImageUri);
  57. if (TextUtils.isEmpty(imageUri)){
  58. return resaveImageFromInputStream(context,contactImageUri);
  59. }
  60. return Uri.parse(imageUri);
  61. }
  62. public static void rotateFileAndReplace(String path) {
  63. try {
  64. BitmapFactory.Options optionsChangeSize = new BitmapFactory.Options();
  65. Bitmap mBitmap = BitmapFactory.decodeFile(path, optionsChangeSize);
  66. ExifInterface mExif = new ExifInterface(path);
  67. int orientation = mExif.getAttributeInt(
  68. ExifInterface.TAG_ORIENTATION, -1);
  69. int exifOrientation = 0;
  70. switch (orientation) {
  71.  
  72. case ExifInterface.ORIENTATION_ROTATE_270:
  73. exifOrientation = 270;
  74. break;
  75.  
  76. case ExifInterface.ORIENTATION_ROTATE_180:
  77. exifOrientation = 180;
  78. break;
  79.  
  80. case ExifInterface.ORIENTATION_ROTATE_90:
  81. exifOrientation = 90;
  82. break;
  83.  
  84. case ExifInterface.ORIENTATION_NORMAL:
  85. exifOrientation = 0;
  86. break;
  87. }
  88.  
  89. if (exifOrientation != 0) {
  90. mBitmap = rotateAndMirror(mBitmap, exifOrientation, false);
  91. mBitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(
  92. path));
  93. }
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. private Uri resaveImageFromInputStream(Context context,Uri contactImageUri){
  99.  
  100. InputStream photoStream = null;
  101. try {
  102. photoStream = context.getContentResolver().openInputStream(contactImageUri);
  103. File tmp = File.createTempFile("phototmp", ".png");
  104. FileOutputStream fos = new FileOutputStream(tmp);
  105. int len = 0;
  106. byte[]buf = new byte[10*1024];
  107. while ((len=photoStream.read(buf))>0){
  108. fos.write(buf,0,len);
  109. }
  110. photoStream.close();
  111. fos.close();
  112. return Uri.fromFile(tmp);
  113. } catch (FileNotFoundException e) {
  114. e.printStackTrace();
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. return null;
  119. }
  120. public static String getPath(Context ctx, Uri uri) {
  121. Cursor cursor = null;
  122. int column_index = 0;
  123. String path = null;
  124.  
  125. try {
  126. String[] projection = { MediaStore.Images.Media.DATA };
  127. cursor = ctx.getContentResolver().query(uri, projection, null,
  128. null, null);
  129. column_index = cursor
  130. .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  131. cursor.moveToFirst();
  132. path = cursor.getString(column_index);
  133. } catch (Exception e){
  134. e.printStackTrace();
  135. }
  136. finally {
  137. if (cursor != null) {
  138. cursor.close();
  139. }
  140. }
  141. return path;
  142. }
  143. public static Bitmap rotateAndMirror(Bitmap b, int degrees, boolean mirror) {
  144. if ((degrees != 0 || mirror) && b != null) {
  145. Matrix m = new Matrix();
  146. // Mirror first.
  147. // horizontal flip + rotation = -rotation + horizontal flip
  148. if (mirror) {
  149. m.postScale(-1, 1);
  150. degrees = (degrees + 360) % 360;
  151. if (degrees == 0 || degrees == 180) {
  152. m.postTranslate(b.getWidth(), 0);
  153. } else if (degrees == 90 || degrees == 270) {
  154. m.postTranslate(b.getHeight(), 0);
  155. } else {
  156. throw new IllegalArgumentException("Invalid degrees="
  157. + degrees);
  158. }
  159. }
  160. if (degrees != 0) {
  161. // clockwise
  162. m.postRotate(degrees, (float) b.getWidth() / 2,
  163. (float) b.getHeight() / 2);
  164. }
  165.  
  166. try {
  167. Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
  168. b.getHeight(), m, true);
  169. if (b != b2) {
  170. b.recycle();
  171. b = b2;
  172. }
  173. } catch (OutOfMemoryError ex) {
  174. ex.printStackTrace();
  175. // We have no memory to rotate. Return the original bitmap.
  176. }
  177. }
  178. return b;
  179. }
  180.  
  181. @Override
  182. public int describeContents() {
  183. return 0;
  184. }
  185.  
  186. @Override
  187. public void writeToParcel(Parcel dest, int flags) {
  188. dest.writeParcelable(this.mContactPickerUri, flags);
  189. }
  190.  
  191. public PhotoPickerHelper(Parcel in) {
  192. this.mContactPickerUri = in.readParcelable(Uri.class.getClassLoader());
  193. }
  194.  
  195. public PhotoPickerHelper() {
  196. }
  197.  
  198. public static Parcelable.Creator<PhotoPickerHelper> CREATOR = new Parcelable.Creator<PhotoPickerHelper>() {
  199. public PhotoPickerHelper createFromParcel(Parcel source) {
  200. return new PhotoPickerHelper(source);
  201. }
  202.  
  203. public PhotoPickerHelper[] newArray(int size) {
  204. return new PhotoPickerHelper[size];
  205. }
  206. };
  207.  
  208. public static int getOrientation(Context ctx, Uri uri) {
  209. int orientation = 0;
  210. Cursor cursor = null;
  211. try {
  212. String[] projection = { MediaStore.Images.Media.ORIENTATION };
  213. cursor = ctx.getContentResolver().query(uri, projection, null,
  214. null, null);
  215. int column_index = cursor
  216. .getColumnIndex(MediaStore.Images.Media.ORIENTATION);
  217. cursor.moveToFirst();
  218. orientation = cursor.getInt(column_index);
  219. } catch (Throwable e) {
  220. e.printStackTrace();
  221. } finally {
  222. if (cursor != null) {
  223. cursor.close();
  224. }
  225. }
  226. return orientation;
  227. }
  228.  
  229. public Uri getContactPickerUri() {
  230. return mContactPickerUri;
  231. }
  232.  
  233. public void setContactPickerUri(Uri contactPickerUri) {
  234. mContactPickerUri = contactPickerUri;
  235. }
  236.  
  237. public static Uri createImageMediaFile(Context context,String bucketName) throws IOException {
  238. ContentValues values = new ContentValues();
  239. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
  240. .format(new Date());
  241. values.put(MediaStore.Images.Media.TITLE, timeStamp);
  242. values.put(MediaStore.Images.Media.DISPLAY_NAME, timeStamp);
  243. values.put(MediaStore.Images.Media.DESCRIPTION, "");
  244. values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
  245. values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
  246. values.put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis());
  247. values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
  248. values.put(MediaStore.Images.Media.ORIENTATION, 0);
  249.  
  250. File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  251. dir = new File(dir,bucketName);
  252. if (!dir.exists()){
  253. dir.mkdirs();
  254. }
  255. File f = new File(dir,"IMG_" + timeStamp + ".png");
  256. f.createNewFile();
  257.  
  258. File parent = f.getParentFile();
  259. String path = parent.toString().toLowerCase();
  260. values.put(MediaStore.Images.ImageColumns.BUCKET_ID, path.hashCode());
  261. values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, bucketName);
  262. values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
  263. Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  264. return result;
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement