Last active
May 19, 2020 06:35
-
-
Save iniyanmurugavel/44f7fb31d8704ee7db933b646dab4882 to your computer and use it in GitHub Desktop.
Upload intent.getData() onActivity uri to path url function to get Path
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
| package com.examp.three.common.fileupload; | |
| import android.content.ContentUris; | |
| import android.content.Context; | |
| import android.database.Cursor; | |
| import android.database.DatabaseUtils; | |
| import android.net.Uri; | |
| import android.os.Environment; | |
| import android.provider.DocumentsContract; | |
| import android.provider.MediaStore; | |
| import android.provider.OpenableColumns; | |
| import android.util.Log; | |
| import java.io.BufferedOutputStream; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| public class FileUtils { | |
| private static final String DOCUMENTS_DIR = "documents"; | |
| public static String getPath(final Context context, final Uri uri) { | |
| if (DocumentsContract.isDocumentUri(context, uri)) { | |
| 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)) { | |
| final String id = DocumentsContract.getDocumentId(uri); | |
| if (id != null && id.startsWith("raw:")) { | |
| return id.substring(4); | |
| } | |
| String[] contentUriPrefixesToTry = new String[]{ | |
| "content://downloads/public_downloads", | |
| "content://downloads/my_downloads" | |
| }; | |
| for (String contentUriPrefix : contentUriPrefixesToTry) { | |
| Uri contentUri = null; | |
| if (id != null) { | |
| contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id)); | |
| } | |
| try { | |
| String path = getDataColumn(context, contentUri, null, null); | |
| if (path != null) { | |
| return path; | |
| } | |
| } catch (Exception ignored) { | |
| } | |
| } | |
| String fileName = getFileName(context, uri); | |
| File cacheDir = getDocumentCacheDir(context); | |
| File file = generateFileName(fileName, cacheDir); | |
| String destinationPath = null; | |
| if (file != null) { | |
| destinationPath = file.getAbsolutePath(); | |
| saveFileFromUri(context, uri, destinationPath); | |
| } | |
| return destinationPath; | |
| } | |
| // 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(); | |
| return getDataColumn(context, uri, null, null); | |
| } | |
| // File | |
| else if ("file".equalsIgnoreCase(uri.getScheme())) { | |
| return uri.getPath(); | |
| } | |
| return null; | |
| } | |
| private static boolean isExternalStorageDocument(Uri uri) { | |
| return "com.android.externalstorage.documents".equals(uri.getAuthority()); | |
| } | |
| private static boolean isDownloadsDocument(Uri uri) { | |
| return "com.android.providers.downloads.documents".equals(uri.getAuthority()); | |
| } | |
| private static boolean isMediaDocument(Uri uri) { | |
| return "com.android.providers.media.documents".equals(uri.getAuthority()); | |
| } | |
| private static boolean isGooglePhotosUri(Uri uri) { | |
| return "com.google.android.apps.photos.content".equals(uri.getAuthority()); | |
| } | |
| private static String getDataColumn(Context context, Uri uri, String selection, | |
| String[] selectionArgs) { | |
| final String column = "_data"; | |
| final String[] projection = { | |
| column | |
| }; | |
| try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, | |
| null)) { | |
| if (cursor != null && cursor.moveToFirst()) { | |
| DatabaseUtils.dumpCursor(cursor); | |
| final int column_index = cursor.getColumnIndexOrThrow(column); | |
| return cursor.getString(column_index); | |
| } | |
| } | |
| return null; | |
| } | |
| private static File getDocumentCacheDir(Context context) { | |
| File dir = new File(context.getCacheDir(), DOCUMENTS_DIR); | |
| if (!dir.exists()) { | |
| dir.mkdirs(); | |
| } | |
| logDir(context.getCacheDir()); | |
| logDir(dir); | |
| return dir; | |
| } | |
| private static void logDir(File dir) { | |
| File[] files = dir.listFiles(); | |
| for (File file : files) { | |
| } | |
| } | |
| private static String getFileName(Context context, Uri uri) { | |
| String mimeType = context.getContentResolver().getType(uri); | |
| String filename = null; | |
| if (mimeType == null) { | |
| String path = getPath(context, uri); | |
| if (path == null) { | |
| filename = getName(uri.toString()); | |
| } else { | |
| File file = new File(path); | |
| filename = file.getName(); | |
| } | |
| } else { | |
| Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); | |
| if (returnCursor != null) { | |
| int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); | |
| returnCursor.moveToFirst(); | |
| filename = returnCursor.getString(nameIndex); | |
| returnCursor.close(); | |
| } | |
| } | |
| return filename; | |
| } | |
| public static String getName(String filename) { | |
| if (filename == null) { | |
| return null; | |
| } | |
| int index = filename.lastIndexOf('/'); | |
| return filename.substring(index + 1); | |
| } | |
| private static File generateFileName(String name, File directory) { | |
| if (name == null) { | |
| return null; | |
| } | |
| File file = new File(directory, name); | |
| if (file.exists()) { | |
| String fileName = name; | |
| String extension = ""; | |
| int dotIndex = name.lastIndexOf('.'); | |
| if (dotIndex > 0) { | |
| fileName = name.substring(0, dotIndex); | |
| extension = name.substring(dotIndex); | |
| } | |
| int index = 0; | |
| while (file.exists()) { | |
| index++; | |
| name = fileName + '(' + index + ')' + extension; | |
| file = new File(directory, name); | |
| } | |
| } | |
| try { | |
| if (!file.createNewFile()) { | |
| return null; | |
| } | |
| } catch (IOException e) { | |
| return null; | |
| } | |
| logDir(directory); | |
| return file; | |
| } | |
| private static void saveFileFromUri(Context context, Uri uri, String destinationPath) { | |
| InputStream is = null; | |
| BufferedOutputStream bos = null; | |
| try { | |
| is = context.getContentResolver().openInputStream(uri); | |
| bos = new BufferedOutputStream(new FileOutputStream(destinationPath, false)); | |
| byte[] buf = new byte[1024]; | |
| if (is != null) { | |
| is.read(buf); | |
| } | |
| do { | |
| bos.write(buf); | |
| } while ((is != null ? is.read(buf) : 0) != -1); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| try { | |
| if (is != null) is.close(); | |
| if (bos != null) bos.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
| //Calling method | |
| onActivityResult() | |
| Uri uri = data.getData(); | |
| if (uri != null) { | |
| String filePath = FileUtils.getPath(this, uri); | |
| } | |
| ===================================================================== | |
| /** | |
| * | |
| * @param file to get the file converting file in base64 and upload | |
| * @return - base64 format of file | |
| */ | |
| public static String encodeFileToBase64Binary(File file) { | |
| int size = (int) file.length(); | |
| byte[] bytes = new byte[size]; | |
| try { | |
| BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file)); | |
| buf.read(bytes, 0, bytes.length); | |
| buf.close(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| return Base64.encodeToString(bytes, Base64.NO_WRAP); | |
| } | |
| ============================================================================ | |
| 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 String getPathFromURI(final Context context, 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]; | |
| } | |
| } | |
| // 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 getDataColumn(context, uri, null, null); | |
| } | |
| // File | |
| else if ("file".equalsIgnoreCase(uri.getScheme())) { | |
| return uri.getPath(); | |
| } | |
| return null; | |
| } | |
| private 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 column_index = cursor.getColumnIndexOrThrow(column); | |
| return cursor.getString(column_index); | |
| } | |
| } finally { | |
| if (cursor != null) | |
| cursor.close(); | |
| } | |
| return null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment