Last active
May 9, 2016 12:55
-
-
Save alorma/1feb9e166036aecfe4e5af990f362bdb to your computer and use it in GitHub Desktop.
File Uri
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.net.Uri; | |
public class ContentUriType extends UriType { | |
@Override | |
public String getPath(Context context, Uri uri) { | |
if (isGooglePhotosUri(uri)) { | |
return uri.getLastPathSegment(); | |
} | |
return getDataColumn(context, uri, null, null); | |
} | |
private boolean isGooglePhotosUri(Uri uri) { | |
return "com.google.android.apps.photos.content".equals(uri.getAuthority()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.net.Uri; | |
public class DefaultUriType extends UriType { | |
@Override | |
public String getPath(Context context, Uri uri) { | |
return String.valueOf(uri); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.net.Uri; | |
public class FileUriType extends UriType { | |
@Override | |
public String getPath(Context context, Uri uri) { | |
return uri.getPath(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.TargetApi; | |
import android.content.ContentUris; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Environment; | |
import android.provider.DocumentsContract; | |
import android.provider.MediaStore; | |
import android.support.annotation.Nullable; | |
@TargetApi(Build.VERSION_CODES.KITKAT) | |
public class KitKatDocumentUri extends UriType { | |
public static final String DOCUMENT_TYPE_PRIMARY = "primary"; | |
public static final String DOCUMENT_TYPE_IMAGE = "image"; | |
public static final String DOCUMENT_TYPE_VIDEO = "video"; | |
public static final String DOCUMENT_TYPE_AUDIO = "audio"; | |
@Override | |
public String getPath(Context context, Uri uri) { | |
if (isExternalStorageDocument(uri)) { | |
final String image = getFromExternalStorage(uri); | |
if (image != null) return image; | |
} else if (isDownloadsDocument(uri)) { | |
return getFromDownloads(context, uri); | |
} else if (isMediaDocument(uri)) { | |
return getFromMediaDocument(context, uri); | |
} | |
return String.valueOf(uri); | |
} | |
private String getFromMediaDocument(Context context, Uri uri) { | |
final String docId = DocumentsContract.getDocumentId(uri); | |
final String[] split = docId.split(":"); | |
final String type = split[0]; | |
Uri contentUri = null; | |
if (DOCUMENT_TYPE_IMAGE.equals(type)) { | |
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; | |
} else if (DOCUMENT_TYPE_VIDEO.equals(type)) { | |
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; | |
} else if (DOCUMENT_TYPE_AUDIO.equals(type)) { | |
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; | |
} | |
final String selection = "_id=?"; | |
final String[] selectionArgs = new String[] { | |
split[1] | |
}; | |
return getDataColumn(context, contentUri, selection, selectionArgs); | |
} | |
private String getFromDownloads(Context context, Uri uri) { | |
final String id = DocumentsContract.getDocumentId(uri); | |
final Uri contentUri = | |
ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), | |
Long.valueOf(id)); | |
return getDataColumn(context, contentUri, null, null); | |
} | |
@Nullable | |
private String getFromExternalStorage(Uri uri) { | |
final String docId = DocumentsContract.getDocumentId(uri); | |
final String[] split = docId.split(":"); | |
final String type = split[0]; | |
if (DOCUMENT_TYPE_PRIMARY.equalsIgnoreCase(type)) { | |
return Environment.getExternalStorageDirectory() + "/" + split[1]; | |
} | |
return null; | |
} | |
public static boolean isExternalStorageDocument(Uri uri) { | |
return "com.android.externalstorage.documents".equals(uri.getAuthority()); | |
} | |
public static boolean isDownloadsDocument(Uri uri) { | |
return "com.android.providers.downloads.documents".equals(uri.getAuthority()); | |
} | |
public static boolean isMediaDocument(Uri uri) { | |
return "com.android.providers.media.documents".equals(uri.getAuthority()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.database.Cursor; | |
import android.net.Uri; | |
public abstract class UriType { | |
public abstract String getPath(Context context, Uri uri); | |
/** | |
* Get the value of the data column for this Uri. This is useful for | |
* MediaStore Uris, and other file-based ContentProviders. | |
* | |
* @param context The context. | |
* @param uri The Uri to query. | |
* @param selection (Optional) Filter used in the query. | |
* @param selectionArgs (Optional) Selection arguments used in the query. | |
* @return The value of the _data column, which is typically a file path. | |
*/ | |
public static String getDataColumn(Context context, Uri uri, String selection, | |
String[] selectionArgs) { | |
Cursor cursor = null; | |
final String column = "_data"; | |
final String[] projection = { | |
column | |
}; | |
try { | |
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); | |
if (cursor != null && cursor.moveToFirst()) { | |
final int index = cursor.getColumnIndexOrThrow(column); | |
return cursor.getString(index); | |
} | |
} finally { | |
if (cursor != null) { | |
cursor.close(); | |
} | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.TargetApi; | |
import android.content.ContentResolver; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.provider.DocumentsContract; | |
public class UriUtils { | |
/** | |
* Get a file path from a Uri. | |
* | |
* @see <a href="http://stackoverflow.com/questions/19834842/android-gallery-on-kitkat-returns-different-uri-for-intent-action-get-content">stackoverflow.com</a> | |
*/ | |
@TargetApi(Build.VERSION_CODES.KITKAT) | |
public static String getPath(Context context, Uri uri) throws NullPointerException{ | |
UriType uriType = new DefaultUriType(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri( | |
context, uri)) { | |
uriType = new KitKatDocumentUri(); | |
} else if (ContentResolver.SCHEME_CONTENT.equalsIgnoreCase(uri.getScheme())) { | |
uriType = new ContentUriType(); | |
} else if (ContentResolver.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) { | |
uriType = new FileUriType(); | |
} | |
return uriType.getPath(context, uri); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment