currently I am facing an issue of not able to retrieve the actual file path of pdf. I tried all the solutions I got, but not able to get the actual file path in Android Pie. Any help will be appreciable.
Code:- Intent to open file manager and allow to select pdf file.
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
String[] mimetypes = {"application/pdf"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(Intent.createChooser(intent, "Choose Pdf"), REQUEST_CODE_SELECT_IMAGE);
After clicking/selecting any pdf file compiler moves to onActivityResult:-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SELECT_IMAGE:
String template_file_uri = null;
String extension = null;
switch (resultCode) {
case Activity.RESULT_OK:
try {
String path = null;
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
Uri selectedFileUri = data.getData();
String mimeType = getContentResolver().getType(selectedFileUri);
Log.e(TAG, "onActivityResult() REQUEST_CODE_SELECT_IMAGE RESULT_OK uri : " + selectedFileUri + " mimetype " + mimeType);
Log.e(TAG, ":::>>selectedFileUri::: " + selectedFileUri);
int lastDot = selectedFileUri.toString().lastIndexOf('.');
if (lastDot == -1) {
// No dots - what do you want to do?
ApplicationHelper.showToast(activity, "Please select only pdf file !!!");
} else {
extension = selectedFileUri.toString().substring(lastDot);
Log.e(TAG, "extension: " + extension);
}
if (extension.equals(".pdf") || mimeType.equals("application/pdf")) {
template_file_uri = selectedFileUri.toString();
displayFromUri(selectedFileUri);
getRealPathFromURI = ApplicationHelper.getRealPathFromURI(activity, selectedFileUri);
} else {
Log.e(TAG, "else ext: " + extension);
ApplicationHelper.showToast(activity, "Please select only pdf file !!!");
template_file_uri = null;
}
Log.e(TAG, "::::>>>getRealPathFromURI::: " + getRealPathFromURI);
} else {
Uri selectedFileUri = data.getData();
String path1 = getPath(activity, selectedFileUri);
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case Activity.RESULT_CANCELED:
template_file_uri = null;
break;
default:
break;
}
break;
default:
break;
}
}
getRealPathFromURI method:-
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getRealPathFromURI(Context context, Uri uri) {
Log.i(TAG, "getRealPathFromURI() uri " + uri);
String pattern = "/^[ A-Za-z0-9_@./#&+-]*$/";
uri = Uri.parse(uri.toString().replaceAll(pattern, uri.toString()));
String path = FileUtils.getPath(context, uri);
Log.i(TAG, "getRealPathFromURI() get path " + path);
if (ApplicationHelper.isStringValid(path)) {
if (!path.contains("://")) {
path = "file://" + path;
}
path = path.replaceAll(pattern, path);
//path = path.replace(" ", "%20");
} else {
path = uri.toString();
}
Log.i(TAG, "getRealPathFromURI() return path " + path);
return path;
}
FileUtils:-
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
Log.i(TAG, "getPath() uri " + uri);
if (DEBUG)
Log.d(TAG + " File -",
"Authority: " + uri.getAuthority() +
", Fragment: " + uri.getFragment() +
", Port: " + uri.getPort() +
", Query: " + uri.getQuery() +
", Scheme: " + uri.getScheme() +
", Host: " + uri.getHost() +
", Segments: " + uri.getPathSegments().toString()
);
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isInternalStorageDocument(uri)) {
return uri.toString();
}
// DocumentProvider
else if (isKitKat && 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];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
try {
final String id = DocumentsContract.getDocumentId(uri);
// final long id = Long.parseLong(DocumentsContract.getDocumentId(uri));
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
Log.e(TAG, "Uri....");
return getDataColumn(context, contentUri, null, null);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
// 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();
} else if (!ApplicationHelper.haveAuthority(uri)) {
return Constants.AUTHORITY_FILE + uri.toString();
}
return null;
}
InputStream
? – PaynterString getRealPathFromURI
andString path1
variables for? – PaynterUri
- dont even try to get "some real path name" as there is no such thing for a genericUri
- more here – Paynter