Reputation: 175
I want to get the file extension of a file I picked with Intent.ACTION_OPEN_DOCUMENT. How to get it?
This is how I start the picker:
private void startFilePicker(){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, CODE_ACTION_OPEN_DOCUMENT_FILE);
}
This is callback when file is picked:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//region New Picker
if (requestCode == CODE_ACTION_OPEN_DOCUMENT_FILE){
if (resultCode == RESULT_OK){
if (data != null){
if (data.getData() != null){
Uri uri = data.getData();
//Get file extension here.
}
}
}
}
}
When I try to get file path by doing uri.getPath()
, I get something like image:105571
, or acc=1;doc=9204
. So I can't get it from path.
The question is how to determine the file extension of picked file?
Upvotes: 1
Views: 2394
Reputation: 1
Retrieve a file's MIME type
A file's data type indicates to the client app how it should handle the file's contents. To get the data type of a shared file given its content URI, the client app calls ContentResolver.getType(). This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.
The following code snippet demonstrates how a client app retrieves the MIME type of a file once the server app has returned the content URI to the client:
/*
* Get the file's content URI from the incoming Intent, then
* get the file's MIME type
*/
Uri returnUri = returnIntent.getData();
String mimeType = getContentResolver().getType(returnUri);
From here: Retrieving file information
Upvotes: 0
Reputation:
Create this two method...
public String getPath(Context context, Uri uri) throws URISyntaxException {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public String getFileExtension(String filePath){
String extension = "";
try{
extension = filePath.substring(filePath.lastIndexOf("."));
}catch(Exception exception){
e("Err", exception.toString()+"");
}
return extension;
}
then use this..
if (data.getData() != null){
try{
Uri uri = data.getData();
//Get file extension here.
String filePath = getPath(YourActivity.this, uri);
String fileExtension = getFileExtension(filePath);
File file = new File(filePath);
}catch(Exception e){
e("Err", e.toString()+"");
}
}
Upvotes: 5
Reputation: 11214
Call the content resolver and do a query() on the uri for display name.
All solutions with path, get path or get real path from uri are bull shit.
Upvotes: 0