0

Instead of the hard coded storage location in the code below, I would like to get the [name & path] of any Excelsheet that user selects, for data import operation. Found that getExternalStorageDirectory is deprecated, not sure how to achieve the requirement for accessing Excelfile from both Internal / External storage of Android.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data == null)
        return;

    switch (requestCode) {
   
        case imrequestcode:

        // Need help at this LOC where filepath could be user selected one.

        String FilePath = "/mnt/sdcard/" + "sampleinput.xls";
    
            try {
                if (resultCode == RESULT_OK) {
                   
                    //// Import function goes here
                }
            } catch (Exception ex) {
               
                lbl.setText("Error " + e);
            }
            break;
    }

}

Intent : Pick an excel sheet which has inputdata

 bimport.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent fileintent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

                fileintent.addCategory(Intent.CATEGORY_OPENABLE);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            MY_PERMISSIONS_LOCATION_ACCESS);

                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            MY_PERMISSIONS_READ_EXTERNAL_STORAGE);

                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
                }
           
                fileintent.setType("application/vnd.ms-excel");

                try {
                    startActivityForResult(fileintent, importrequestcode);

                    fileintent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);

                    if (Build.VERSION.SDK_INT > 22) {
                        requestPermissions(new String[]{"FLAG_GRANT_READ_URI_PERMISSION"}, 11);

                    }

                } catch (ActivityNotFoundException e) {
                    lbl.setText("No file picker activity.");
                }
            }

        });

    }
MdBasha
  • 423
  • 4
  • 16

1 Answers1

0

To get the Filename:

private String getFileName(Uri uri) {

        Cursor mCursor =
                getApplicationContext().getContentResolver().query(uri, null, null, null, null);
        int indexedname = mCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        mCursor.moveToFirst();
        String filename = mCursor.getString(indexedname);
        mCursor.close();
        return filename;
    }

To get the FilePath:

[Checkthislink](https://stackoverflow.com/questions/13209494/how-to-get-the-full-file-path-from-uri/55469368#55469368)

Call the method and class in onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data == null)
        return;
        
    switch (requestCode) {
   
        case imrequestcode:
     
         Uri fileuri = data.getData();
         String Nameoffile_selected = getFileName(fileuri);
         String Pathoffile_selected = FileUtils.getPath(this, fileuri);
    
            try {
                if (resultCode == RESULT_OK) {
                   
                    //// Import function goes here
                }
            } catch (Exception ex) {
               
                lbl.setText("Error " + e);
            }
            break;
    }
}

MdBasha
  • 423
  • 4
  • 16