1

I'm trying to let a user export an excel file in my app and have the app automatically open the created excel file. The excel file is created and stored successfully using jxl, but when I try to open it with the Hancom Office Editor nothing happens other than my screen getting a dark overlay. Here's a gif:
enter image description here
I can't figure out what would cause this and can't find anything about it online. I'm using the accepted answer from here to support my target SDK 28.

my export method:

 public void onExport(View view){
        try {
            //write changes to excel file(changes made outide of function)
            workbook.write();
            workbook.close();
            Toast.makeText(getApplication(),
                    "Data Exported to Excel Sheet", Toast.LENGTH_SHORT).show();
            findViewById(R.id.btn_export).setEnabled(false);

            //set file to saved excel file
            File file = new File(Environment.getExternalStorageDirectory(),
                    race.getName()+".xls");
            Uri path = FileProvider.getUriForFile(getApplicationContext(), "com.something.racecalculator.fileprovider", file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Log.i("path: ", path.toString());
            intent.setDataAndType(path, "image/jpg");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
        catch (ActivityNotFoundException e) {
            Log.i("oh no:", "No application available to view excel");
        }
 }

My provider tag in AndroidManifest.xml(set as a child of ):

<provider
            android:name=".GenericFileProvider"
            android:authorities="com.something.racecalculator.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

I'm not getting any Errors or warnings as far as I can tell. Thanks for any help.

Alex Charters
  • 301
  • 2
  • 12
  • 2
    why you set setDataAndType image/jpg ? it should be application/vnd.ms-excel – Ahsan Malik Aug 06 '18 at 04:39
  • 1
    @AhsanMalik oh man, I can't believe I didn't see that! I used it earlier to see if I could open images to determine if it was just excel files that I couldn't open. That very well could have been the entire problem. Anyway, I eventually fixed it and it is now correct in the answer I posted. – Alex Charters Aug 06 '18 at 04:44
  • chill dear enjoy – Ahsan Malik Aug 06 '18 at 04:55

1 Answers1

4

I'm not sure exactly what caused the problem, however, I suspect that I might have had the file path wrong. I changed a couple things (including where the excel files were being saved to. Changed from /storage/emulated/0/ to /storage/emulated/0/Samsung) and here is what worked:

Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File("/storage/emulated/0/Samsung"+File.separator + race.getName()+".xls");
Uri path = FileProvider.getUriForFile(getApplicationContext(), "com.something.racecalculator.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(path, "application/vnd.ms-excel");
startActivity(intent);

remember that you need to implement a FileProvider if your target SDK is 24 or above

Alex Charters
  • 301
  • 2
  • 12