Search code examples
androidandroid-fragmentsandroid-afilechooser

aFileChooser error, get file path shows error in fragment


public void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {
    case REQUEST_CHOOSER:   
        if (resultCode == Activity.RESULT_OK) {

            final Uri uri = data.getData();

            // Get the File path from the Uri
            String path = FileUtils.getPath(this, uri);

            // Alternatively, use FileUtils.getFile(Context, Uri)
            if (path != null && FileUtils.isLocal(path)) {
                File file = new File(path);
            }
        }
        break;
     }
   }

I'm copying this code from github,this link https://github.com/iPaulPro/aFileChooser,I'm putting this code inside a fragment. In this line, it shows error

 String path = FileUtils.getPath(this, uri);

Showing this error:

The method getPath(Context, Uri) in the type FileUtils is not applicable for the arguments (PagesFragment, Uri)

Anyone can help me solve this problem?


Solution

  • try like this,

     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        switch (requestCode) {
            case REQUEST_CHOOSER:   
                if (resultCode == Activity.RESULT_OK) {
    
                    final Uri uri = data.getData();
    
                    // Get the File path from the Uri
                    String path = FileUtils.getPath(getActivity(), uri);
    
                    // Alternatively, use FileUtils.getFile(Context, Uri)
                    if (path != null && FileUtils.isLocal(path)) {
                        File file = new File(path);
                    }
                }
                break;
             }
           }