2 * ownCloud Android client application
4 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.utils
;
22 import android
.annotation
.TargetApi
;
23 import android
.content
.ContentUris
;
24 import android
.content
.Context
;
25 import android
.database
.Cursor
;
26 import android
.net
.Uri
;
27 import android
.os
.Build
;
28 import android
.os
.Environment
;
29 import android
.provider
.DocumentsContract
;
30 import android
.provider
.MediaStore
;
31 import android
.provider
.OpenableColumns
;
33 import com
.owncloud
.android
.MainApp
;
34 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
40 * A helper class for some Uri operations.
42 public class UriUtils
{
44 public static final String TAG
= UriUtils
.class.getSimpleName();
46 public static final String URI_CONTENT_SCHEME
= "content://";
50 * Get the value of the data column for this Uri. This is useful for
51 * MediaStore Uris, and other file-based ContentProviders.
53 * @param context The context.
54 * @param uri The Uri to query.
55 * @param selection (Optional) Filter used in the query.
56 * @param selectionArgs (Optional) Selection arguments used in the query.
57 * @return The value of the _data column, which is typically a file path.
59 public static String
getDataColumn(Context context
, Uri uri
, String selection
, String
[] selectionArgs
) {
62 final String column
= "_data";
63 final String
[] projection
= { column
};
66 cursor
= context
.getContentResolver().query(uri
, projection
, selection
, selectionArgs
, null
);
67 if (cursor
!= null
&& cursor
.moveToFirst()) {
69 final int column_index
= cursor
.getColumnIndexOrThrow(column
);
70 return cursor
.getString(column_index
);
80 * @param uri The Uri to check.
81 * @return Whether the Uri authority is ExternalStorageProvider.
83 public static boolean isExternalStorageDocument(Uri uri
) {
84 return "com.android.externalstorage.documents".equals(uri
.getAuthority());
88 * @param uri The Uri to check.
89 * @return Whether the Uri authority is DownloadsProvider.
91 public static boolean isDownloadsDocument(Uri uri
) {
92 return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
96 * @param uri The Uri to check.
97 * @return Whether the Uri authority is MediaProvider.
99 public static boolean isMediaDocument(Uri uri
) {
100 return "com.android.providers.media.documents".equals(uri
.getAuthority());
104 * @param uri The Uri to check.
105 * @return Whether the Uri authority is Google Photos.
107 public static boolean isGooglePhotosUri(Uri uri
) {
108 return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
113 * @param uri The Uri to check.
114 * @return Whether the Uri is from a content provider as kind "content://..."
116 public static boolean isContentDocument(Uri uri
) {
117 return uri
.toString().startsWith(URI_CONTENT_SCHEME
);
122 * Translates a content:// URI referred to a local file file to a path on the local filesystem
124 * @param uri The URI to resolve
125 * @return The path in the file system to the content or null if it could not be found (not a file)
127 @TargetApi(Build
.VERSION_CODES
.KITKAT
)
128 public static String
getLocalPath(Uri uri
, Context context
) {
129 final boolean isKitKatOrLater
= Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.KITKAT
;
132 if (isKitKatOrLater
&& DocumentsContract
.isDocumentUri(context
, uri
)) {
133 // ExternalStorageProvider
134 if (UriUtils
.isExternalStorageDocument(uri
)) {
135 final String docId
= DocumentsContract
.getDocumentId(uri
);
136 final String
[] split
= docId
.split(":");
137 final String type
= split
[0];
139 if ("primary".equalsIgnoreCase(type
)) {
140 return Environment
.getExternalStorageDirectory() + "/" + split
[1];
144 else if (UriUtils
.isDownloadsDocument(uri
)) {
146 final String id
= DocumentsContract
.getDocumentId(uri
);
147 final Uri contentUri
= ContentUris
.withAppendedId(Uri
.parse("content://downloads/public_downloads"),
150 return UriUtils
.getDataColumn(context
, contentUri
, null
, null
);
153 else if (UriUtils
.isMediaDocument(uri
)) {
154 final String docId
= DocumentsContract
.getDocumentId(uri
);
155 final String
[] split
= docId
.split(":");
156 final String type
= split
[0];
158 Uri contentUri
= null
;
159 if ("image".equals(type
)) {
160 contentUri
= MediaStore
.Images
.Media
.EXTERNAL_CONTENT_URI
;
161 } else if ("video".equals(type
)) {
162 contentUri
= MediaStore
.Video
.Media
.EXTERNAL_CONTENT_URI
;
163 } else if ("audio".equals(type
)) {
164 contentUri
= MediaStore
.Audio
.Media
.EXTERNAL_CONTENT_URI
;
167 final String selection
= "_id=?";
168 final String
[] selectionArgs
= new String
[] { split
[1] };
170 return UriUtils
.getDataColumn(context
, contentUri
, selection
, selectionArgs
);
172 // Documents providers returned as content://...
173 else if (UriUtils
.isContentDocument(uri
)) {
174 return uri
.toString();
177 // MediaStore (and general)
178 else if ("content".equalsIgnoreCase(uri
.getScheme())) {
180 // Return the remote address
181 if (UriUtils
.isGooglePhotosUri(uri
))
182 return uri
.getLastPathSegment();
184 return UriUtils
.getDataColumn(context
, uri
, null
, null
);
187 else if ("file".equalsIgnoreCase(uri
.getScheme())) {
188 return uri
.getPath();