There is no "real file". You did not show what you used to get this result, but presumably it was ACTION_GET_CONTENT . That, or ACTION_OPEN_DOCUMENT , are not limited to files on the filesystem that you can access. They can return a Uri that points to anything that the content provider wants: encrypted content, files in inaccessible locations, BLOB columns, content that needs to be downloaded, and so on. The user chooses the content, not you, and the user might choose from any of these types of sources.
so that i can upload it to server
Find some HTTP API that supports uploading from a Uri .
Or, use ContentResolver and openInputStream() to get an InputStream on the content, then find some HTTP API that supports uploading from an InputStream .
Or, use ContentResolver and openInputStream() to get an InputStream on the content, make a copy of that content to some temporary file (e.g., in getCacheDir() ), upload from your temporary file, then delete the temporary file when the download is complete.
answered Sep 12, 2016 at 16:23 CommonsWare CommonsWare 1.0m 192 192 gold badges 2.4k 2.4k silver badges 2.6k 2.6k bronze badges Oh okay . may be i was getting something wrong but i have been able to solve it Commented Sep 12, 2016 at 19:20fun getPath(uri: Uri): String? < val isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) < // ExternalStorageProvider if (isExternalStorageDocument(uri)) < val docId = DocumentsContract.getDocumentId(uri); val split = docId.split(":"); val type = split[0]; if ("primary".equals(type, true)) < return Environment.getExternalStorageDirectory().path + "/" + split[1]; >// TODO handle non-primary volumes > // DownloadsProvider else if (isDownloadsDocument(uri)) < val val contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); >// MediaProvider else if (isMediaDocument(uri)) < val docId = DocumentsContract.getDocumentId(uri); val split = docId.split(":"); val type = split[0]; var contentUri: Uri? = null; if ("image" == type) < contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; >else if ("video" == type) < contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; >else if ("audio" == type) < contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; >val selection = "_id=?"; val selectionArgs = arrayOf(split[1]) return getDataColumn(context, contentUri, selection, selectionArgs); > > // MediaStore (and general) else if ("content".equals(uri.getScheme(), true)) < // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); >// File else if ("file".equals(uri.getScheme(), true)) < return uri.getPath(); >return null; > fun getDataColumn(context: Context, uri: Uri?, selection: String?, selectionArgs: Array?): String? < var cursor: Cursor? = null; val column = "_data"; val projection = arrayOf(column); try < cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) < val index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); >> finally < if (cursor != null) cursor.close(); >return null; > fun isExternalStorageDocument(uri: Uri): Boolean < return "com.android.externalstorage.documents" == uri.authority; >/** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ fun isDownloadsDocument(uri: Uri): Boolean < return "com.android.providers.downloads.documents" == uri.authority; >/** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ fun isMediaDocument(uri: Uri): Boolean < return "com.android.providers.media.documents" == uri.authority; >/** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ fun isGooglePhotosUri(uri: Uri): Booleananswered Aug 26, 2017 at 8:48 Lalit Jadav Lalit Jadav 1,427 1 1 gold badge 15 15 silver badges 38 38 bronze badges
I got it resolved after copying that pdf file to cache directory Please check the complete solution here:
public static final String DOCUMENTS_DIR = "documents"; if (isDownloadsDocument(uri)) < final String 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 = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id)); try < String path = getDataColumn(context, contentUri, null, null); if (path != null) < return path; >> catch (Exception e) <> > // path could not be retrieved using ContentResolver, therefore copy file to accessible cache using streams 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; > public static String getFileName(@NonNull Context context, Uri uri) < String mimeType = context.getContentResolver().getType(uri); String filename = null; if (mimeType == null && context != 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); > public static File getDocumentCacheDir(@NonNull Context context) < File dir = new File(context.getCacheDir(), DOCUMENTS_DIR); if (!dir.exists()) < dir.mkdirs(); >// logDir(context.getCacheDir()); // logDir(dir); return dir; > @Nullable public static File generateFileName(@Nullable 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) < //Log.w(TAG, 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]; is.read(buf); do < bos.write(buf); >while (is.read(buf) != -1); > catch (IOException e) < e.printStackTrace(); >finally < try < if (is != null) is.close(); if (bos != null) bos.close(); >catch (IOException e) < e.printStackTrace(); >> >
Appreciated the help on this thread