1

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?

Swifty Swift
  • 175
  • 1
  • 1
  • 7
  • if someone needs a mime type instead, you can use: context.getContentResolver().getType(uri) – haart Jan 27 '21 at 15:46

2 Answers2

5

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()+"");
    }
}
  • Thanks! This solved my problem, but I have another quick question about this subject. What is a good way to get the File itself? I am using File file = new File(uri.getPath()); Is this safe? – Swifty Swift Oct 26 '17 at 07:55
  • File file = new File(filePath); –  Oct 26 '17 at 10:45
  • Thanks again! However there is one problem with this, I cant get Uri or files I select from Google Drive. Any solutions to this? – Swifty Swift Oct 26 '17 at 10:47
  • When you are using Google Drive its not showing file from your device storage. So you can't choose them using normal Intent. Try this link https://stackoverflow.com/questions/18188653/how-to-select-files-from-google-drive-in-android –  Oct 26 '17 at 10:52
0

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.

greenapps
  • 11,154
  • 2
  • 16
  • 19