0

I am not geting currect path var name "FilePath" im geting value E/File path: /document/29 but my selected file stored in downloads folder file name is "test.xlsx" i need original path with file name with file extention to pass in FileInputStream().I am not able to fix it ...can anybody give the code

btnimport.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
                fileintent.setType("*/*");
                try {
                    startActivityForResult(fileintent, requestcode);
                } catch (ActivityNotFoundException e) {
                    lbl.setText("No activity can handle picking a file. Showing alternatives.");
                }




protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null)
            return;
        switch (requestCode) {
            case requestcode:
                String FilePath = data.getData().getPath();

                Log.e("File path", FilePath);

                if (FilePath.contains("/root_path"))
                    FilePath = FilePath.replace("/root_path", "");

                Log.e("New File path", FilePath);

                try {
                    if (resultCode == RESULT_OK) {
                        AssetManager am = this.getAssets();
                        InputStream inStream;
                        Workbook wb = null;

                        try {
                            inStream = new FileInputStream(FilePath);
                            Log.e("Extension",FilePath.substring(FilePath.lastIndexOf(".")));

                            if (FilePath.substring(FilePath.lastIndexOf(".")).equals(".xls")) {
                                Log.e("File Type", "Selected file is XLS");
                                wb = new HSSFWorkbook(inStream);
                            }
                            else if (FilePath.substring(FilePath.lastIndexOf(".")).equals(".xlsx")) {
                                Log.e("File Type", "Selected file is XLSX");
                                wb = new XSSFWorkbook(inStream);
                            }
                            else {
                                wb = null;
                                lbl.setText("Please select a valid Excel file");
                                return;

                            }

                            inStream.close();

1 Answers1

0

i need original path with file name with file extention to pass in FileInputStream().

No you do not need 'a real path' as you better use the obtained uri to open an input stream.

InputStream is = getContentResolver().openInputStream(data.getData());

Use that stream as if it was your wanted stream.

blackapps
  • 8,011
  • 2
  • 11
  • 25