Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1.  
  2. public class FileUtils {
  3.  
  4. public static boolean isLocal(String url) {
  5. if (url != null && !url.startsWith("http://") && !url.startsWith("https://")) {
  6. return true;
  7. }
  8. return false;
  9. }
  10.  
  11. /**
  12. * @param uri The Uri to check.
  13. * @return Whether the Uri authority is ExternalStorageProvider.
  14. * @author paulburke
  15. */
  16. public static boolean isExternalStorageDocument(Uri uri) {
  17. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  18. }
  19.  
  20. /**
  21. * @param uri The Uri to check.
  22. * @return Whether the Uri authority is DownloadsProvider.
  23. * @author paulburke
  24. */
  25. public static boolean isDownloadsDocument(Uri uri) {
  26. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  27. }
  28.  
  29. /**
  30. * @param uri The Uri to check.
  31. * @return Whether the Uri authority is MediaProvider.
  32. * @author paulburke
  33. */
  34. public static boolean isMediaDocument(Uri uri) {
  35. return "com.android.providers.media.documents".equals(uri.getAuthority());
  36. }
  37.  
  38. /**
  39. * @param uri The Uri to check.
  40. * @return Whether the Uri authority is Old Google Photos.
  41. */
  42. public static boolean isGoogleOldPhotosUri(Uri uri) {
  43. return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  44. }
  45.  
  46. /**
  47. * @param uri The Uri to check.
  48. * @return Whether the Uri authority is New Google Photos.
  49. */
  50. public static boolean isGoogleNewPhotosUri(Uri uri) {
  51. return "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority());
  52. }
  53.  
  54. public static String getDataColumn(Context context, Uri uri, String selection,
  55. String[] selectionArgs) {
  56.  
  57. Cursor cursor = null;
  58. final String column = "_data";
  59. final String[] projection = {
  60. column
  61. };
  62.  
  63. try {
  64. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  65. null);
  66. if (cursor != null && cursor.moveToFirst()) {
  67. final int column_index = cursor.getColumnIndexOrThrow(column);
  68. return cursor.getString(column_index);
  69. }
  70. } finally {
  71. if (cursor != null)
  72. cursor.close();
  73. }
  74. return null;
  75. }
  76.  
  77. public static String getPath(final Context context, final Uri uri) {
  78. // DocumentProvider
  79. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  80. if(DocumentsContract.isDocumentUri(context, uri)) {
  81. // ExternalStorageProvider
  82. if (isExternalStorageDocument(uri)) {
  83. final String docId = DocumentsContract.getDocumentId(uri);
  84. final String[] split = docId.split(":");
  85. final String type = split[0];
  86.  
  87. if ("primary".equalsIgnoreCase(type)) {
  88. return Environment.getExternalStorageDirectory() + "/" + split[1];
  89. }
  90.  
  91. // TODO handle non-primary volumes
  92. }
  93. // DownloadsProvider
  94. else if (isDownloadsDocument(uri)) {
  95.  
  96. final String id = DocumentsContract.getDocumentId(uri);
  97. final Uri contentUri = ContentUris.withAppendedId(
  98. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  99.  
  100. return getDataColumn(context, contentUri, null, null);
  101. }
  102. // MediaProvider
  103. else if (isMediaDocument(uri)) {
  104. final String docId = DocumentsContract.getDocumentId(uri);
  105. final String[] split = docId.split(":");
  106. final String type = split[0];
  107.  
  108. Uri contentUri = null;
  109. if ("image".equals(type)) {
  110. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  111. }
  112.  
  113. final String selection = "_id=?";
  114. final String[] selectionArgs = new String[]{
  115. split[1]
  116. };
  117.  
  118. return getDataColumn(context, contentUri, selection, selectionArgs);
  119. }
  120. }
  121. // MediaStore (and general)
  122. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  123.  
  124. // Return the remote address
  125. if (isGoogleOldPhotosUri(uri)) {
  126. // return http path, then download file.
  127. return uri.getLastPathSegment();
  128. }
  129. else if (isGoogleNewPhotosUri(uri)) {
  130. if(getDataColumn(context, uri, null, null) == null) {
  131. return getDataColumn(context, Uri.parse(getImageUrlWithAuthority(context,uri)), null, null);
  132. }else{
  133. return getDataColumn(context, uri, null, null);
  134. }
  135. }
  136.  
  137. return getDataColumn(context, uri, null, null);
  138. }
  139. // File
  140. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  141. return uri.getPath();
  142. }
  143. }else{
  144. String[] proj = { MediaStore.Images.Media.DATA };
  145. String result = null;
  146.  
  147. CursorLoader cursorLoader = new CursorLoader(
  148. context,
  149. uri, proj, null, null, null);
  150. Cursor cursor = cursorLoader.loadInBackground();
  151.  
  152. if(cursor != null){
  153. int column_index =
  154. cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  155. cursor.moveToFirst();
  156. result = cursor.getString(column_index);
  157. }
  158.  
  159. return result;
  160. }
  161.  
  162.  
  163. return null;
  164. }
  165.  
  166. public static File getFile(Context context, Uri uri) {
  167. if (uri != null) {
  168. String path = getPath(context, uri);
  169. if (path != null && isLocal(path)) {
  170. return new File(path);
  171. }
  172. }
  173. return null;
  174. }
  175.  
  176. public static String getImageUrlWithAuthority(Context context, Uri uri) {
  177. InputStream is = null;
  178. if (uri.getAuthority() != null) {
  179. try {
  180. is = context.getContentResolver().openInputStream(uri);
  181. Bitmap bmp = BitmapFactory.decodeStream(is);
  182.  
  183. return writeToTempImageAndGetPathUri(context, bmp).toString();
  184. } catch (FileNotFoundException e) {
  185. e.printStackTrace();
  186. }finally {
  187. try {
  188. is.close();
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. }
  194. return null;
  195. }
  196.  
  197. public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
  198. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  199. inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  200. String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  201. return Uri.parse(path);
  202. }
  203. }
  204.  
  205. File files = FileUtils.getFile(context, uri);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement