Created
January 22, 2019 19:27
-
-
Save mmdock/ed04315851534029f5bc6ea77d6dc6e4 to your computer and use it in GitHub Desktop.
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
public static String getPath(Context context, Uri uri) { | |
final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19; | |
String selection = null; | |
String[] selectionArgs = null; | |
// Uri is different in versions after KITKAT (Android 4.4), we need to | |
// deal with different Uris. | |
if (needToCheckUri && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) { | |
if (isExternalStorageDocument(uri)) { | |
final String docId = DocumentsContract.getDocumentId(uri); | |
final String[] split = docId.split(":"); | |
return Environment.getExternalStorageDirectory() + "/" + split[1]; | |
} else if (isDownloadsDocument(uri)) { | |
final String id = DocumentsContract.getDocumentId(uri); | |
uri = ContentUris.withAppendedId( | |
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); | |
} else if (isMediaDocument(uri)) { | |
final String docId = DocumentsContract.getDocumentId(uri); | |
final String[] split = docId.split(":"); | |
final String type = split[0]; | |
if ("image".equals(type)) { | |
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; | |
} else if ("video".equals(type)) { | |
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; | |
} else if ("audio".equals(type)) { | |
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; | |
} | |
selection = "_id=?"; | |
selectionArgs = new String[]{ split[1] }; | |
} | |
} | |
if ("content".equalsIgnoreCase(uri.getScheme())) { | |
if (isGoogleOldPhotosUri(uri)) { | |
return uri.getLastPathSegment(); | |
} else if (isGoogleNewPhotosUri(uri) || isPicasaPhotoUri(uri)) { | |
Uri returnUri = getImageUrlWithAuthority(context, uri); | |
if (returnUri!=null) return returnUri.getPath(); | |
} | |
String[] projection = { MediaStore.Images.Media.DATA }; | |
try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) { | |
if (cursor != null) { | |
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); | |
if (cursor.moveToFirst()) { | |
return cursor.getString(column_index); | |
} | |
} | |
} catch (IllegalArgumentException e) { | |
Timber.e(e); | |
} | |
} else if ("file".equalsIgnoreCase(uri.getScheme())) { | |
Uri returnUri = getImageUrlWithAuthority(context, uri); | |
if (returnUri!=null) return returnUri.getPath(); | |
} | |
return null; | |
} | |
private static Uri getImageUrlWithAuthority(Context context, Uri uri) { | |
if (uri.getAuthority() != null) { | |
Bitmap b = decodeSampledBitmapFromUri(uri, context); | |
if (b!=null) return writeToTempImageAndGetPathUri(context, b); | |
} | |
return null; | |
} | |
public static Uri copyContentToTempFile(Uri contentUri, Context context) throws NullPointerException { | |
InputStream inputStream = null; | |
OutputStream outputStream = null; | |
File outputFile = new File(context.getCacheDir(), UUID.randomUUID().toString() + "-temp.jpg"); | |
try { | |
inputStream = context.getContentResolver().openInputStream(contentUri); | |
outputStream = new FileOutputStream(outputFile); | |
if (inputStream == null) { | |
throw new NullPointerException("InputStream for given input Uri is null"); | |
} | |
byte buffer[] = new byte[1024]; | |
int length; | |
while ((length = inputStream.read(buffer)) > 0) { | |
outputStream.write(buffer, 0, length); | |
} | |
return Uri.fromFile(outputFile); | |
} catch (FileNotFoundException e) { | |
return contentUri; | |
} catch (IOException e) { | |
return contentUri; | |
} finally { | |
try { | |
if (outputStream != null) outputStream.close(); | |
if (inputStream != null) inputStream.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) { | |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | |
inImage.compress(Bitmap.CompressFormat.JPEG, 90, bytes); | |
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); | |
return Uri.parse(path); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is ExternalStorageProvider. | |
*/ | |
private static boolean isExternalStorageDocument(Uri uri) { | |
return "com.android.externalstorage.documents".equals(uri.getAuthority()); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is DownloadsProvider. | |
*/ | |
private static boolean isDownloadsDocument(Uri uri) { | |
return "com.android.providers.downloads.documents".equals(uri.getAuthority()); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is MediaProvider. | |
*/ | |
private static boolean isMediaDocument(Uri uri) { | |
return "com.android.providers.media.documents".equals(uri.getAuthority()); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is the old Google Photos. | |
*/ | |
private static boolean isGoogleOldPhotosUri(Uri uri) { | |
return "com.google.android.apps.photos.content".equals(uri.getAuthority()); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is the new Google Photos. | |
*/ | |
private static boolean isGoogleNewPhotosUri(Uri uri) { | |
return "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority()); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is Picasa. | |
*/ | |
private static boolean isPicasaPhotoUri(Uri uri) { | |
return uri != null | |
&& !TextUtils.isEmpty(uri.getAuthority()) | |
&& (uri.getAuthority().startsWith("com.android.gallery3d") | |
|| uri.getAuthority().startsWith("com.google.android.gallery3d")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment