Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.55 KB | None | 0 0
  1. import android.annotation.SuppressLint;
  2. import android.annotation.TargetApi;
  3. import android.app.Activity;
  4. import android.content.ContentUris;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.graphics.Bitmap;
  9. import android.net.Uri;
  10. import android.os.Build;
  11. import android.os.Environment;
  12. import android.provider.DocumentsContract;
  13. import android.provider.MediaStore;
  14. import android.webkit.MimeTypeMap;
  15.  
  16. import com.example.jonathan.medsblaprueba.exceptions.NoExtensionException;
  17. import com.example.jonathan.medsblaprueba.models.MyMessage;
  18.  
  19. import java.io.ByteArrayOutputStream;
  20.  
  21. /**
  22. * Created by jonathan on 27/04/17.
  23. */
  24. public class UtilsFilePath {
  25.  
  26.  
  27. private static final String IMAGE_MIME = "image";
  28. private static final String VIDEO_MIME = "video";
  29. private static final String PDF_MIME = "pdf";
  30. private static final String DOC_MIME = "doc";
  31. private static final String DOCX_MIME = "docx";
  32.  
  33. public static void openGallery(Activity activity, int requestCode) {
  34. Intent i = new Intent(Intent.ACTION_PICK,
  35. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  36. i.setType("image/* video/*");
  37. activity.startActivityForResult(i, requestCode);
  38. }
  39.  
  40. public static void openFileChooser(Activity activity, int requestCode) {
  41. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  42. intent.setType("*/*");
  43. intent.addCategory(Intent.CATEGORY_OPENABLE);
  44. activity.startActivityForResult(intent, requestCode);
  45. }
  46.  
  47. /**
  48. * Method for return file path of Gallery image
  49. *
  50. * @param context
  51. * @param uri
  52. * @return path of the selected image file from gallery
  53. */
  54. static String noPath = "Select Video Only";
  55.  
  56. @TargetApi(Build.VERSION_CODES.KITKAT)
  57. @SuppressLint("NewApi")
  58. public static String getPath(final Context context, final Uri uri) {
  59.  
  60. // check here to KITKAT or new version
  61. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  62.  
  63. // DocumentProvider
  64. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  65.  
  66. // ExternalStorageProvider
  67. if (isExternalStorageDocument(uri)) {
  68. final String docId = DocumentsContract.getDocumentId(uri);
  69. final String[] split = docId.split(":");
  70. final String type = split[0];
  71.  
  72. if ("primary".equalsIgnoreCase(type)) {
  73. return Environment.getExternalStorageDirectory() + "/"
  74. + split[1];
  75. }
  76. }
  77. // DownloadsProvider
  78. else if (isDownloadsDocument(uri)) {
  79.  
  80. final String id = DocumentsContract.getDocumentId(uri);
  81. final Uri contentUri = ContentUris.withAppendedId(
  82. Uri.parse("content://downloads/public_downloads"),
  83. Long.valueOf(id));
  84.  
  85. return getDataColumn(context, contentUri, null, null);
  86. }
  87. // MediaProvider
  88. else if (isMediaDocument(uri)) {
  89. final String docId = DocumentsContract.getDocumentId(uri);
  90. final String[] split = docId.split(":");
  91. final String type = split[0];
  92.  
  93. Uri contentUri = null;
  94. if ("image".equals(type)) {
  95. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  96. } else if ("video".equals(type)) {
  97. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  98. } else if ("audio".equals(type)) {
  99. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  100. }
  101.  
  102. final String selection = "_id=?";
  103. final String[] selectionArgs = new String[] { split[1] };
  104.  
  105. return getDataColumn(context, contentUri, selection,
  106. selectionArgs);
  107. }
  108. }
  109. // MediaStore (and general)
  110. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  111.  
  112. // Return the remote address
  113. if (isGooglePhotosUri(uri))
  114. return uri.getLastPathSegment();
  115.  
  116. return getDataColumn(context, uri, null, null);
  117. }
  118. // File
  119. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  120. return uri.getPath();
  121. }
  122.  
  123. return noPath;
  124. }
  125.  
  126. /**
  127. * Get the value of the data column for this Uri. This is <span id="IL_AD2"
  128. * class="IL_AD">useful</span> for MediaStore Uris, and other file-based
  129. * ContentProviders.
  130. *
  131. * @param context
  132. * The context.
  133. * @param uri
  134. * The Uri to query.
  135. * @param selection
  136. * (Optional) Filter used in the query.
  137. * @param selectionArgs
  138. * (Optional) Selection arguments used in the query.
  139. * @return The value of the _data column, which is typically a file path.
  140. */
  141. public static String getDataColumn(Context context, Uri uri,
  142. String selection, String[] selectionArgs) {
  143.  
  144. Cursor cursor = null;
  145. final String column = "_data";
  146. final String[] projection = { column };
  147.  
  148. try {
  149. cursor = context.getContentResolver().query(uri, projection,
  150. selection, selectionArgs, null);
  151. if (cursor != null && cursor.moveToFirst()) {
  152. final int index = cursor.getColumnIndexOrThrow(column);
  153. return cursor.getString(index);
  154. }
  155. } finally {
  156. if (cursor != null)
  157. cursor.close();
  158. }
  159. return noPath;
  160. }
  161.  
  162. /**
  163. * @param uri
  164. * The Uri to check.
  165. * @return Whether the Uri authority is ExternalStorageProvider.
  166. */
  167. public static boolean isExternalStorageDocument(Uri uri) {
  168. return "com.android.externalstorage.documents".equals(uri
  169. .getAuthority());
  170. }
  171.  
  172. /**
  173. * @param uri
  174. * The Uri to check.
  175. * @return Whether the Uri authority is DownloadsProvider.
  176. */
  177. public static boolean isDownloadsDocument(Uri uri) {
  178. return "com.android.providers.downloads.documents".equals(uri
  179. .getAuthority());
  180. }
  181.  
  182. /**
  183. * @param uri
  184. * The Uri to check.
  185. * @return Whether the Uri authority is MediaProvider.
  186. */
  187. public static boolean isMediaDocument(Uri uri) {
  188. return "com.android.providers.media.documents".equals(uri
  189. .getAuthority());
  190. }
  191.  
  192. /**
  193. * @param uri
  194. * The Uri to check.
  195. * @return Whether the Uri authority is Google Photos.
  196. */
  197. public static boolean isGooglePhotosUri(Uri uri) {
  198. return "com.google.android.apps.photos.content".equals(uri
  199. .getAuthority());
  200. }
  201.  
  202. // public static String getMimeType(String filePath) {
  203. // String type = null;
  204. // String extension = MimeTypeMap.getFileExtensionFromUrl(filePath);
  205. // if (extension != null) {
  206. // type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
  207. // }
  208. // return type;
  209. // }
  210.  
  211. public static boolean isPDFTypeFromPath(String path) throws NoExtensionException {
  212. return getMimeType(path).contains(UtilsFilePath.PDF_MIME);
  213. }
  214.  
  215. /*
  216. public static boolean isImageType(String path) throws NoExtensionException {
  217. return path != null && path.contains(UtilsFilePath.IMAGE_MIME);
  218. //return getMimeType(path).contains(UtilsFilePath.IMAGE_MIME);
  219. }
  220. */
  221.  
  222. public static boolean isImageTypeFromPath(String path) throws NoExtensionException {
  223. return getMimeType(path).contains(UtilsFilePath.IMAGE_MIME);
  224. }
  225.  
  226. public static boolean isVideoTypeFromPath(String path) throws NoExtensionException {
  227. return getMimeType(path).contains(UtilsFilePath.VIDEO_MIME);
  228. }
  229.  
  230. public static boolean isDocTypeFromPath(String path) throws NoExtensionException {
  231. return getMimeType(path).contains(UtilsFilePath.DOC_MIME) || getMimeType(path).contains(UtilsFilePath.DOCX_MIME);
  232. }
  233.  
  234. public static int getTypeFromPath(String urlFile) {
  235. try {
  236. if (isImageTypeFromPath(urlFile)){
  237. return MyMessage.FILE_IMAGE_TYPE;
  238. }
  239. if (isVideoTypeFromPath(urlFile)){
  240. return MyMessage.FILE_VIDEO_TYPE;
  241. }
  242. if (UtilsFilePath.isDocTypeFromPath(urlFile)){
  243. return MyMessage.FILE_DOC_TYPE;
  244. }
  245. if (UtilsFilePath.isPDFTypeFromPath(urlFile)){
  246. return MyMessage.FILE_PDF_TYPE;
  247. }
  248. } catch (NoExtensionException e) {
  249. e.printStackTrace();
  250. }
  251. return MyMessage.FILE_OTHER_TYPE;
  252. }
  253.  
  254. public static String getMimeType(String path) throws NoExtensionException {
  255. String extension = MimeTypeMap.getFileExtensionFromUrl(path);
  256. if (extension.isEmpty()){
  257. throw new NoExtensionException();
  258. }
  259. return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
  260. }
  261.  
  262. public static Uri getImageUri(Context inContext, Bitmap inImage) {
  263. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  264. inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  265. String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  266. return Uri.parse(path);
  267. }
  268.  
  269. public static String getFileName(String pathImage) {
  270. String fileName = pathImage.substring(pathImage.lastIndexOf("/")+1);
  271. return fileName.isEmpty() ? "" : fileName;
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement