UriUtils.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2015 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.owncloud.android.utils;
  19. import android.content.ContentResolver;
  20. import android.content.Context;
  21. import android.database.Cursor;
  22. import android.net.Uri;
  23. import android.provider.MediaStore;
  24. import android.webkit.MimeTypeMap;
  25. import com.owncloud.android.lib.common.utils.Log_OC;
  26. import java.util.Locale;
  27. /**
  28. * A helper class for some Uri operations.
  29. */
  30. public final class UriUtils {
  31. private static final String TAG = UriUtils.class.getSimpleName();
  32. public static final String URI_CONTENT_SCHEME = "content://";
  33. private UriUtils() {
  34. // utility class -> private constructor
  35. }
  36. public static String getDisplayNameForUri(Uri uri, Context context) {
  37. if (uri == null || context == null) {
  38. throw new IllegalArgumentException("Received NULL!");
  39. }
  40. String displayName;
  41. if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
  42. displayName = uri.getLastPathSegment(); // ready to return
  43. } else {
  44. // content: URI
  45. displayName = getDisplayNameFromContentResolver(uri, context);
  46. try {
  47. if (displayName == null) {
  48. // last chance to have a name
  49. displayName = uri.getLastPathSegment().replaceAll("\\s", "");
  50. }
  51. // Add best possible extension
  52. int index = displayName.lastIndexOf('.');
  53. if (index == -1 || MimeTypeMap.getSingleton().
  54. getMimeTypeFromExtension(displayName.substring(index + 1).toLowerCase(Locale.ROOT)) == null) {
  55. String mimeType = context.getContentResolver().getType(uri);
  56. String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
  57. if (extension != null) {
  58. displayName += "." + extension;
  59. }
  60. }
  61. } catch (Exception e) {
  62. Log_OC.e(TAG, "No way to get a display name for " + uri.toString());
  63. }
  64. }
  65. // Replace path separator characters to avoid inconsistent paths
  66. return displayName.replaceAll("/", "-");
  67. }
  68. private static String getDisplayNameFromContentResolver(Uri uri, Context context) {
  69. String displayName = null;
  70. String mimeType = context.getContentResolver().getType(uri);
  71. if (mimeType != null) {
  72. String displayNameColumn;
  73. if (MimeTypeUtil.isImage(mimeType)) {
  74. displayNameColumn = MediaStore.Images.ImageColumns.DISPLAY_NAME;
  75. } else if (MimeTypeUtil.isVideo(mimeType)) {
  76. displayNameColumn = MediaStore.Video.VideoColumns.DISPLAY_NAME;
  77. } else if (MimeTypeUtil.isAudio(mimeType)) {
  78. displayNameColumn = MediaStore.Audio.AudioColumns.DISPLAY_NAME;
  79. } else {
  80. displayNameColumn = MediaStore.Files.FileColumns.DISPLAY_NAME;
  81. }
  82. try (Cursor cursor = context.getContentResolver().query(
  83. uri,
  84. new String[]{displayNameColumn},
  85. null,
  86. null,
  87. null
  88. )) {
  89. if (cursor != null) {
  90. cursor.moveToFirst();
  91. displayName = cursor.getString(cursor.getColumnIndex(displayNameColumn));
  92. }
  93. } catch (Exception e) {
  94. Log_OC.e(TAG, "Could not retrieve display name for " + uri.toString());
  95. // nothing else, displayName keeps null
  96. }
  97. }
  98. return displayName;
  99. }
  100. }