diff --git a/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModule.java b/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModule.java index 48fb5c1..5ca1e20 100644 --- a/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModule.java +++ b/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModule.java @@ -3,6 +3,7 @@ package com.imagepicker; import android.Manifest; import android.app.Activity; import android.content.ActivityNotFoundException; +import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -69,6 +70,7 @@ public class ImagePickerModule extends ReactContextBaseJavaModule public static final int REQUEST_LAUNCH_IMAGE_LIBRARY = 13002; public static final int REQUEST_LAUNCH_VIDEO_LIBRARY = 13003; public static final int REQUEST_LAUNCH_VIDEO_CAPTURE = 13004; + public static final int REQUEST_LAUNCH_MIXED_CAPTURE = 13005; public static final int REQUEST_PERMISSIONS_FOR_CAMERA = 14001; public static final int REQUEST_PERMISSIONS_FOR_LIBRARY = 14002; @@ -265,27 +267,22 @@ public class ImagePickerModule extends ReactContextBaseJavaModule { cameraIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, videoDurationLimit); } - } - else - { + } else if (pickBoth) { + Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + this.setImageCaptureUri(takePictureIntent); + Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); + cameraIntent = new Intent(Intent.ACTION_CHOOSER); + Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); + Intent[] intentArray = new Intent[]{takePictureIntent,takeVideoIntent}; + cameraIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); + cameraIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action"); + cameraIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); + requestCode = REQUEST_LAUNCH_MIXED_CAPTURE; + } else { requestCode = REQUEST_LAUNCH_IMAGE_CAPTURE; cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - final File original = createNewFile(reactContext, this.options, false); - imageConfig = imageConfig.withOriginalFile(original); - - if (imageConfig.original != null) { - cameraCaptureURI = RealPathUtil.compatUriFromFile(reactContext, imageConfig.original); - }else { - responseHelper.invokeError(callback, "Couldn't get file path for photo"); - return; - } - if (cameraCaptureURI == null) - { - responseHelper.invokeError(callback, "Couldn't get file path for photo"); - return; - } - cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraCaptureURI); + this.setImageCaptureUri(cameraIntent); } if (cameraIntent.resolveActivity(reactContext.getPackageManager()) == null) @@ -349,17 +346,18 @@ public class ImagePickerModule extends ReactContextBaseJavaModule requestCode = REQUEST_LAUNCH_VIDEO_LIBRARY; libraryIntent = new Intent(Intent.ACTION_PICK); libraryIntent.setType("video/*"); - } - else - { + } else if (pickBoth) { + libraryIntent = new Intent(Intent.ACTION_GET_CONTENT); + libraryIntent.addCategory(Intent.CATEGORY_OPENABLE); + libraryIntent.setType("image/*"); + libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"}); + requestCode = REQUEST_LAUNCH_MIXED_CAPTURE; + } else { requestCode = REQUEST_LAUNCH_IMAGE_LIBRARY; libraryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); - if (pickBoth) - { - libraryIntent.setType("image/* video/*"); - } + libraryIntent.setType("image/*"); } if (libraryIntent.resolveActivity(reactContext.getPackageManager()) == null) @@ -385,75 +383,42 @@ public class ImagePickerModule extends ReactContextBaseJavaModule } } - @Override - public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { - //robustness code - if (passResult(requestCode)) - { - return; - } - - responseHelper.cleanResponse(); - - // user cancel - if (resultCode != Activity.RESULT_OK) - { - removeUselessFiles(requestCode, imageConfig); - responseHelper.invokeCancel(callback); - callback = null; - return; - } - - Uri uri = null; - switch (requestCode) - { - case REQUEST_LAUNCH_IMAGE_CAPTURE: - uri = cameraCaptureURI; - break; - - case REQUEST_LAUNCH_IMAGE_LIBRARY: - uri = data.getData(); - String realPath = getRealPathFromURI(uri); - final boolean isUrl = !TextUtils.isEmpty(realPath) && - Patterns.WEB_URL.matcher(realPath).matches(); - if (realPath == null || isUrl) - { - try - { - File file = createFileFromURI(uri); - realPath = file.getAbsolutePath(); - uri = Uri.fromFile(file); - } - catch (Exception e) - { - // image not in cache - responseHelper.putString("error", "Could not read photo"); - responseHelper.putString("uri", uri.toString()); - responseHelper.invokeResponse(callback); - callback = null; - return; - } - } - imageConfig = imageConfig.withOriginalFile(new File(realPath)); - break; - - case REQUEST_LAUNCH_VIDEO_LIBRARY: - responseHelper.putString("uri", data.getData().toString()); - responseHelper.putString("path", getRealPathFromURI(data.getData())); - responseHelper.invokeResponse(callback); - callback = null; - return; + protected String getMimeType(Activity activity, Uri uri) { + String mimeType = null; + if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { + ContentResolver cr = activity.getApplicationContext().getContentResolver(); + mimeType = cr.getType(uri); + } else { + String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); + mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension( + fileExtension.toLowerCase()); + } + return mimeType; + } - case REQUEST_LAUNCH_VIDEO_CAPTURE: - final String path = getRealPathFromURI(data.getData()); - responseHelper.putString("uri", data.getData().toString()); - responseHelper.putString("path", path); - fileScan(reactContext, path); + protected void extractImageFromResult(Activity activity, Uri uri, int requestCode) { + String realPath = getRealPathFromURI(uri); + String mime = getMimeType(activity, uri); + final boolean isUrl = !TextUtils.isEmpty(realPath) && + Patterns.WEB_URL.matcher(realPath).matches(); + if (isUrl) { + try { + File file = createFileFromURI(uri); + realPath = file.getAbsolutePath(); + uri = Uri.fromFile(file); + } catch (Exception e) { + // image not in cache + responseHelper.putString("error", "Could not read photo"); + responseHelper.putString("uri", uri.toString()); responseHelper.invokeResponse(callback); callback = null; + this.options = null; return; + } } + imageConfig = imageConfig.withOriginalFile(new File(realPath)); + final ReadExifResult result = readExifInterface(responseHelper, imageConfig); if (result.error != null) @@ -461,6 +426,7 @@ public class ImagePickerModule extends ReactContextBaseJavaModule removeUselessFiles(requestCode, imageConfig); responseHelper.invokeError(callback, result.error.getMessage()); callback = null; + this.options = null; return; } @@ -472,7 +438,7 @@ public class ImagePickerModule extends ReactContextBaseJavaModule updatedResultResponse(uri, imageConfig.original.getAbsolutePath()); // don't create a new file if contraint are respected - if (imageConfig.useOriginal(initialWidth, initialHeight, result.currentRotation)) + if (imageConfig.useOriginal(initialWidth, initialHeight, result.currentRotation) || mime.equals("image/gif")) { responseHelper.putInt("width", initialWidth); responseHelper.putInt("height", initialHeight); @@ -481,6 +447,13 @@ public class ImagePickerModule extends ReactContextBaseJavaModule else { imageConfig = getResizedImage(reactContext, this.options, imageConfig, initialWidth, initialHeight, requestCode); + if (imageConfig == null) + { + responseHelper.invokeError(callback, "Could not read image"); + callback = null; + this.options = null; + return; + } if (imageConfig.resized == null) { removeUselessFiles(requestCode, imageConfig); @@ -523,6 +496,61 @@ public class ImagePickerModule extends ReactContextBaseJavaModule this.options = null; } + protected void extractVideoFromResult(Uri uri) { + responseHelper.putString("uri", uri.toString()); + responseHelper.putString("path", getRealPathFromURI(uri)); + responseHelper.invokeResponse(callback); + callback = null; + this.options = null; + } + + @Override + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { + //robustness code + if (passResult(requestCode)) + { + return; + } + + responseHelper.cleanResponse(); + + // user cancel + if (resultCode != Activity.RESULT_OK) + { + removeUselessFiles(requestCode, imageConfig); + responseHelper.invokeCancel(callback); + callback = null; + return; + } + + switch (requestCode) + { + case REQUEST_LAUNCH_IMAGE_CAPTURE: + extractImageFromResult(activity, cameraCaptureURI, requestCode); + break; + case REQUEST_LAUNCH_IMAGE_LIBRARY: + extractImageFromResult(activity, data.getData(), requestCode); + break; + case REQUEST_LAUNCH_VIDEO_LIBRARY: + extractVideoFromResult(data.getData()); + break; + case REQUEST_LAUNCH_MIXED_CAPTURE: + case REQUEST_LAUNCH_VIDEO_CAPTURE: + if (data == null || data.getData() == null) { + extractImageFromResult(activity, cameraCaptureURI, requestCode); + } else { + Uri selectedMediaUri = data.getData(); + String mime = getMimeType(activity, selectedMediaUri); + if (mime.contains("image")) { + extractImageFromResult(activity, selectedMediaUri, requestCode); + } else { + extractVideoFromResult(selectedMediaUri); + } + } + break; + } + } + public void invokeCustomButton(@NonNull final String action) { responseHelper.invokeCustomButton(this.callback, action); @@ -551,7 +579,8 @@ public class ImagePickerModule extends ReactContextBaseJavaModule { return callback == null || (cameraCaptureURI == null && requestCode == REQUEST_LAUNCH_IMAGE_CAPTURE) || (requestCode != REQUEST_LAUNCH_IMAGE_CAPTURE && requestCode != REQUEST_LAUNCH_IMAGE_LIBRARY - && requestCode != REQUEST_LAUNCH_VIDEO_LIBRARY && requestCode != REQUEST_LAUNCH_VIDEO_CAPTURE); + && requestCode != REQUEST_LAUNCH_VIDEO_LIBRARY && requestCode != REQUEST_LAUNCH_VIDEO_CAPTURE + && requestCode != REQUEST_LAUNCH_MIXED_CAPTURE); } private void updatedResultResponse(@Nullable final Uri uri, @@ -571,22 +600,24 @@ public class ImagePickerModule extends ReactContextBaseJavaModule @NonNull final Callback callback, @NonNull final int requestCode) { - final int writePermission = ActivityCompat - .checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); - final int cameraPermission = ActivityCompat - .checkSelfPermission(activity, Manifest.permission.CAMERA); - - boolean permissionsGranted = false; + int selfCheckResult = 0; switch (requestCode) { case REQUEST_PERMISSIONS_FOR_LIBRARY: - permissionsGranted = writePermission == PackageManager.PERMISSION_GRANTED; + selfCheckResult = ActivityCompat + .checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); break; case REQUEST_PERMISSIONS_FOR_CAMERA: - permissionsGranted = cameraPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED; + selfCheckResult = ActivityCompat + .checkSelfPermission(activity, Manifest.permission.CAMERA); + if (selfCheckResult == PackageManager.PERMISSION_GRANTED) { + selfCheckResult = ActivityCompat + .checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); + } break; } + final boolean permissionsGranted = selfCheckResult == PackageManager.PERMISSION_GRANTED; if (!permissionsGranted) { final Boolean dontAskAgain = ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) && ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA); @@ -787,4 +818,21 @@ public class ImagePickerModule extends ReactContextBaseJavaModule videoDurationLimit = options.getInt("durationLimit"); } } + + private void setImageCaptureUri(Intent cameraIntent) { + final File original = createNewFile(reactContext, this.options, false); + imageConfig = imageConfig.withOriginalFile(original); + + if (imageConfig.original != null) { + cameraCaptureURI = RealPathUtil.compatUriFromFile(reactContext, imageConfig.original); + } else { + responseHelper.invokeError(callback, "Couldn't get file path for photo"); + return; + } + if (cameraCaptureURI == null) { + responseHelper.invokeError(callback, "Couldn't get file path for photo"); + return; + } + cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraCaptureURI); + } } diff --git a/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/utils/RealPathUtil.java b/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/utils/RealPathUtil.java index cc90dce..72ddc92 100644 --- a/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/utils/RealPathUtil.java +++ b/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/utils/RealPathUtil.java @@ -1,201 +1,270 @@ package com.imagepicker.utils; -import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.net.Uri; -import android.os.Build; import android.provider.DocumentsContract; import android.provider.MediaStore; -import android.content.ContentUris; +import android.provider.OpenableColumns; +import android.content.ContentResolver; import android.os.Environment; +import android.webkit.MimeTypeMap; +import android.util.Log; +import android.text.TextUtils; + +import android.os.ParcelFileDescriptor; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.FileProvider; -import java.io.File; +import java.io.*; +import java.nio.channels.FileChannel; +import java.util.Objects; + +// Class based on the steveevers DocumentHelper https://gist.github.com/steveevers/a5af24c226f44bb8fdc3 public class RealPathUtil { + public static final String CACHE_DIR_NAME = "mmShare"; + + public static @Nullable + Uri compatUriFromFile(@NonNull final Context context, + @NonNull final File file) { + Uri result = null; + final String packageName = context.getApplicationContext().getPackageName(); + final String authority = packageName + ".provider"; + try { + result = FileProvider.getUriForFile(context, authority, file); + } + catch(IllegalArgumentException e) { + e.printStackTrace(); + } + return result; + } + + public static String getRealPathFromURI(final Context context, final Uri uri) { + + // DocumentProvider + if (DocumentsContract.isDocumentUri(context, uri)) { + // ExternalStorageProvider + if (isExternalStorageDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + + if ("primary".equalsIgnoreCase(type)) { + return Environment.getExternalStorageDirectory() + "/" + split[1]; + } + } else if (isDownloadsDocument(uri)) { + // DownloadsProvider + + final String id = DocumentsContract.getDocumentId(uri); + if (!TextUtils.isEmpty(id)) { + if (id.startsWith("raw:")) { + return id.replaceFirst("raw:", ""); + } + try { + return getPathFromSavingTempFile(context, uri); + } catch (NumberFormatException e) { + Log.e("ReactNative", "DownloadsProvider unexpected uri " + uri.toString()); + return null; + } + } + } else if (isMediaDocument(uri)) { + // MediaProvider + + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + + Uri contentUri = null; + if ("image".equals(type)) { + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + } else if ("video".equals(type)) { + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; + } else if ("audio".equals(type)) { + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; + } + + final String selection = "_id=?"; + final String[] selectionArgs = new String[] { + split[1] + }; + + if (contentUri != null) { + return getDataColumn(context, contentUri, selection, selectionArgs); + } else { + return getPathFromSavingTempFile(context, uri); + } + } + } + + if ("content".equalsIgnoreCase(uri.getScheme())) { + // MediaStore (and general) + + if (isGooglePhotosUri(uri)) { + return uri.getLastPathSegment(); + } + + // Try save to tmp file, and return tmp file path + return getPathFromSavingTempFile(context, uri); + } else if ("file".equalsIgnoreCase(uri.getScheme())) { + return uri.getPath(); + } + + return null; + } + + public static String getPathFromSavingTempFile(Context context, final Uri uri) { + File tmpFile; + String fileName = null; + + if (uri == null || uri.isRelative()) { + return null; + } + + // Try and get the filename from the Uri + try { + Cursor returnCursor = + context.getContentResolver().query(uri, null, null, null, null); + int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); + returnCursor.moveToFirst(); + fileName = sanitizeFilename(returnCursor.getString(nameIndex)); + returnCursor.close(); + + } catch (Exception e) { + // just continue to get the filename with the last segment of the path + } + + try { + if (TextUtils.isEmpty(fileName)) { + fileName = sanitizeFilename(uri.getLastPathSegment().trim()); + } + + + File cacheDir = new File(context.getCacheDir(), CACHE_DIR_NAME); + if (!cacheDir.exists()) { + cacheDir.mkdirs(); + } + + tmpFile = new File(cacheDir, fileName); + tmpFile.createNewFile(); + + ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); + + FileChannel src = new FileInputStream(pfd.getFileDescriptor()).getChannel(); + FileChannel dst = new FileOutputStream(tmpFile).getChannel(); + dst.transferFrom(src, 0, src.size()); + src.close(); + dst.close(); + } catch (IOException ex) { + return null; + } + return tmpFile.getAbsolutePath(); + } + + 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; + } + + + 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()); + } + + public static boolean isGooglePhotosUri(Uri uri) { + return "com.google.android.apps.photos.content".equals(uri.getAuthority()); + } + + public static String getExtension(String uri) { + if (uri == null) { + return null; + } + + int dot = uri.lastIndexOf("."); + if (dot >= 0) { + return uri.substring(dot); + } else { + // No extension. + return ""; + } + } + + public static String getMimeType(File file) { + + String extension = getExtension(file.getName()); + + if (extension.length() > 0) + return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1)); + + return "application/octet-stream"; + } + + public static String getMimeType(String filePath) { + File file = new File(filePath); + return getMimeType(file); + } + + public static String getMimeTypeFromUri(final Context context, final Uri uri) { + try { + ContentResolver cR = context.getContentResolver(); + return cR.getType(uri); + } catch (Exception e) { + return "application/octet-stream"; + } + } + + public static void deleteTempFiles(final File dir) { + try { + if (dir.isDirectory()) { + deleteRecursive(dir); + } + } catch (Exception e) { + // do nothing + } + } + + private static void deleteRecursive(File fileOrDirectory) { + if (fileOrDirectory.isDirectory()) + for (File child : Objects.requireNonNull(fileOrDirectory.listFiles())) + deleteRecursive(child); + + fileOrDirectory.delete(); + } + + private static String sanitizeFilename(String filename) { + if (filename == null) { + return null; + } - public static @Nullable Uri compatUriFromFile(@NonNull final Context context, - @NonNull final File file) { - Uri result = null; - if (Build.VERSION.SDK_INT < 21) { - result = Uri.fromFile(file); - } - else { - final String packageName = context.getApplicationContext().getPackageName(); - final String authority = new StringBuilder(packageName).append(".provider").toString(); - try { - result = FileProvider.getUriForFile(context, authority, file); - } - catch(IllegalArgumentException e) { - e.printStackTrace(); - } - } - return result; - } - - @SuppressLint("NewApi") - public static @Nullable String getRealPathFromURI(@NonNull final Context context, - @NonNull final Uri uri) { - - final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; - - // DocumentProvider - if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { - // ExternalStorageProvider - if (isExternalStorageDocument(uri)) { - final String docId = DocumentsContract.getDocumentId(uri); - final String[] split = docId.split(":"); - final String type = split[0]; - - if ("primary".equalsIgnoreCase(type)) { - return Environment.getExternalStorageDirectory() + "/" + split[1]; - } - - // TODO handle non-primary volumes - } - // DownloadsProvider - else if (isDownloadsDocument(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); - } - // MediaProvider - else if (isMediaDocument(uri)) { - final String docId = DocumentsContract.getDocumentId(uri); - final String[] split = docId.split(":"); - final String type = split[0]; - - Uri contentUri = null; - if ("image".equals(type)) { - contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; - } else if ("video".equals(type)) { - contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; - } else if ("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); - } - } - // MediaStore (and general) - else if ("content".equalsIgnoreCase(uri.getScheme())) { - - // Return the remote address - if (isGooglePhotosUri(uri)) - return uri.getLastPathSegment(); - - if (isFileProviderUri(context, uri)) - return getFileProviderPath(context, uri); - - return getDataColumn(context, uri, null, null); - } - // File - else if ("file".equalsIgnoreCase(uri.getScheme())) { - return uri.getPath(); - } - - return null; - } - - /** - * 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; - } - - - /** - * @param uri The Uri to check. - * @return Whether the Uri authority is ExternalStorageProvider. - */ - public 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. - */ - public 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. - */ - public 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 Google Photos. - */ - public static boolean isGooglePhotosUri(@NonNull final Uri uri) { - return "com.google.android.apps.photos.content".equals(uri.getAuthority()); - } - - /** - * @param context The Application context - * @param uri The Uri is checked by functions - * @return Whether the Uri authority is FileProvider - */ - public static boolean isFileProviderUri(@NonNull final Context context, - @NonNull final Uri uri) { - final String packageName = context.getPackageName(); - final String authority = new StringBuilder(packageName).append(".provider").toString(); - return authority.equals(uri.getAuthority()); - } - - /** - * @param context The Application context - * @param uri The Uri is checked by functions - * @return File path or null if file is missing - */ - public static @Nullable String getFileProviderPath(@NonNull final Context context, - @NonNull final Uri uri) - { - final File appDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); - final File file = new File(appDir, uri.getLastPathSegment()); - return file.exists() ? file.toString(): null; - } + File f = new File(filename); + return f.getName(); + } }