roko_man

flipImage

Jan 14th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. public class ImageFilePath {
  2.  
  3. /**
  4. * Method for return file path of Gallery image
  5. *
  6. * @param context
  7. * @param uri
  8. * @return path of the selected image file from gallery
  9. */
  10. @SuppressLint("NewApi")
  11. public static String getPath(final Context context, final Uri uri) {
  12.  
  13. // check here to KITKAT or new version
  14. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  15.  
  16. // DocumentProvider
  17. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  18.  
  19. // ExternalStorageProvider
  20. if (isExternalStorageDocument(uri)) {
  21. final String docId = DocumentsContract.getDocumentId(uri);
  22. final String[] split = docId.split(":");
  23. final String type = split[0];
  24.  
  25. if ("primary".equalsIgnoreCase(type)) {
  26. return Environment.getExternalStorageDirectory() + "/"
  27. + split[1];
  28. }
  29. }
  30. // DownloadsProvider
  31. else if (isDownloadsDocument(uri)) {
  32.  
  33. final String id = DocumentsContract.getDocumentId(uri);
  34. final Uri contentUri = ContentUris.withAppendedId(
  35. Uri.parse("content://downloads/public_downloads"),
  36. Long.valueOf(id));
  37.  
  38. return getDataColumn(context, contentUri, null, null);
  39. }
  40. // MediaProvider
  41. else if (isMediaDocument(uri)) {
  42. final String docId = DocumentsContract.getDocumentId(uri);
  43. final String[] split = docId.split(":");
  44. final String type = split[0];
  45.  
  46. Uri contentUri = null;
  47. if ("image".equals(type)) {
  48. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  49. } else if ("video".equals(type)) {
  50. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  51. } else if ("audio".equals(type)) {
  52. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  53. }
  54.  
  55. final String selection = "_id=?";
  56. final String[] selectionArgs = new String[]{split[1]};
  57.  
  58. return getDataColumn(context, contentUri, selection,
  59. selectionArgs);
  60. }
  61. }
  62. // MediaStore (and general)
  63. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  64.  
  65. // Return the remote address
  66. if (isGooglePhotosUri(uri))
  67. return uri.getLastPathSegment();
  68.  
  69. return getDataColumn(context, uri, null, null);
  70. }
  71. // File
  72. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  73. return uri.getPath();
  74. }
  75.  
  76. return null;
  77. }
  78.  
  79. /**
  80. * Get the value of the data column for this Uri. This is useful for
  81. * MediaStore Uris, and other file-based ContentProviders.
  82. *
  83. * @param context The context.
  84. * @param uri The Uri to query.
  85. * @param selection (Optional) Filter used in the query.
  86. * @param selectionArgs (Optional) Selection arguments used in the query.
  87. * @return The value of the _data column, which is typically a file path.
  88. */
  89. public static String getDataColumn(Context context, Uri uri,
  90. String selection, String[] selectionArgs) {
  91.  
  92. Cursor cursor = null;
  93. final String column = "_data";
  94. final String[] projection = {column};
  95.  
  96. try {
  97. cursor = context.getContentResolver().query(uri, projection,
  98. selection, selectionArgs, null);
  99. if (cursor != null && cursor.moveToFirst()) {
  100. final int index = cursor.getColumnIndexOrThrow(column);
  101. return cursor.getString(index);
  102. }
  103. } finally {
  104. if (cursor != null)
  105. cursor.close();
  106. }
  107. return null;
  108. }
  109.  
  110. /**
  111. * @param uri The Uri to check.
  112. * @return Whether the Uri authority is ExternalStorageProvider.
  113. */
  114. public static boolean isExternalStorageDocument(Uri uri) {
  115. return "com.android.externalstorage.documents".equals(uri
  116. .getAuthority());
  117. }
  118.  
  119. /**
  120. * @param uri The Uri to check.
  121. * @return Whether the Uri authority is DownloadsProvider.
  122. */
  123. public static boolean isDownloadsDocument(Uri uri) {
  124. return "com.android.providers.downloads.documents".equals(uri
  125. .getAuthority());
  126. }
  127.  
  128. /**
  129. * @param uri The Uri to check.
  130. * @return Whether the Uri authority is MediaProvider.
  131. */
  132. public static boolean isMediaDocument(Uri uri) {
  133. return "com.android.providers.media.documents".equals(uri
  134. .getAuthority());
  135. }
  136.  
  137. /**
  138. * @param uri The Uri to check.
  139. * @return Whether the Uri authority is Google Photos.
  140. */
  141. public static boolean isGooglePhotosUri(Uri uri) {
  142. return "com.google.android.apps.photos.content".equals(uri
  143. .getAuthority());
  144. }
  145. }
  146.  
  147.  
  148.  
  149.  
  150. ///lip image
  151.  
  152. fun flipImage(path: String): Bitmap {
  153.  
  154. var exif: ExifInterface? = null
  155. var yourSelectedImage: Bitmap
  156.  
  157. try {
  158. exif = ExifInterface(path)
  159. } catch (e: IOException) {
  160. e.printStackTrace()
  161. }
  162.  
  163. val orientation = exif!!.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
  164. val m = Matrix()
  165.  
  166. var rotate = 0
  167.  
  168. if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
  169. rotate = 180
  170.  
  171. if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
  172. rotate = 270
  173.  
  174. if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
  175. rotate = 90
  176.  
  177. m.postRotate(rotate.toFloat())
  178.  
  179. val options = BitmapFactory.Options()
  180. options.inSampleSize = 2
  181. options.inPreferredConfig = Bitmap.Config.RGB_565
  182.  
  183. yourSelectedImage = BitmapFactory.decodeFile(path, options)
  184.  
  185.  
  186. try {
  187. yourSelectedImage = Bitmap.createBitmap(yourSelectedImage, 0, 0,
  188. yourSelectedImage.width,
  189. yourSelectedImage.height, m, true)
  190.  
  191. // image quality - it was set to 500,500
  192. // yourSelectedImage = Bitmap.createScaledBitmap(yourSelectedImage, 1000, 1000, true);
  193. } catch (e: Exception) {
  194. e.printStackTrace()
  195. }
  196.  
  197.  
  198. return yourSelectedImage
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment