0

i just code simple check to my file where it get from uri.getPath() but i dont know why is always can't pass the syntax file.isFile()

    Button      btnChooseFile, btnUploadFile;
    EditText    etFileName;
    String      path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnChooseFile   = findViewById(R.id.btnChooseFile);
        btnUploadFile   = findViewById(R.id.btnUploadFile);
        etFileName      = findViewById(R.id.etFileName);

        ActivityResultLauncher<String> mGetContent = registerForActivityResult(
                new ActivityResultContracts.GetContent(),
                new ActivityResultCallback<Uri>() {
                    @Override
                    public void onActivityResult(Uri result) {
                        etFileName.setText(result.getPath());
                        path = result.toString();
                    }
                });

        btnChooseFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mGetContent.launch("*/*");
            }
        });

        btnUploadFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                File file = new File(path);

                String result;

                if(file.isFile()){
                    result = "This is File";
                }else{
                    result = "This is Not File";
                }
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
            }
        });
    }
  • Your `path` variable `result.toString()` which is not a path . See [This](https://stackoverflow.com/questions/13209494/how-to-get-the-full-file-path-from-uri) or any other question . Follow any latest answer on this . – ADM Feb 22 '22 at 09:17
  • Please, state the exact error message you get instead of a phrase "can't pass the syntax file.isFile()". Compile-time error? Run-time exception? – Ralf Kleberhoff Feb 22 '22 at 09:24
  • @RalfKleberhoff I'm not getting any error, what i mean is I want to making File with ```URI.toString()``` or ```URI.getPath()``` from URI, and use in this syntax ```File file = new File(This Path Here);```, And when its run, this syntax not getting any error, but when i check in ```file.isFile()``` is not recognize as a file i choose, my target is File with extension pdf/doc/ppt – Andi Firdaus Feb 22 '22 at 22:10
  • Assuming that the `path` URI you get really denotes a file and not e.g. an HTTP resource (i.e. uses the `file://` syntax), then `new File(path)` should indeed give you the file. Debug the app, and show us the contents of `path` and `file`. – Ralf Kleberhoff Feb 23 '22 at 10:38

0 Answers0