I want to select pdf file from phone on button click and display its file name on a text view. till now I have done opening file manager for selecting pdf on button click
<
You can do the following:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String fileName = getFileName(this, data.getData());
yourTextView.setText(fileName);
}
public String getFileName(Context context, Uri uri) {
if (uri != null && context != null) {
Cursor returnCursor =
context.getContentResolver().query(uri, null, null, null, null);
if(returnCursor != null) {
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
if(nameIndex >= 0 && sizeIndex >= 0){
Log.d("File Name : " + returnCursor.getString(nameIndex));
Log.d("File Size : " + Long.toString(returnCursor.getLong(sizeIndex)));
Boolean isValidFile = checkFormat(returnCursor.getString(nameIndex));
if(!isValidFile){
return returnCursor.getString(nameIndex);
}
}
private Boolean checkOtherFileType(String filePath){
if(!Utility.isNullOrEmpty(filePath)) {
String filePathInLowerCase = filePath.toLowerCase();
if(filePathInLowerCase.endsWith(".pdf")) {
return true;
}
}
return false;
}
Hope this will help you out