0

What we are trying to do: We are attempting to replicate an existing Java Desktop application into an Android application (based upon Java as well). The desktop application uses the following Desktop class code to open any form of file type (e.g. docx, xlsx, txt, pdf. jpg, etc.) passed to it. This code will open the file to an associated application or prompt the user to select an application if none is associated or exists.

Our problem: Being new to Android development and system classes, we haven't been able to correctly identify if there is such a class in Android or the proper terminology to find it and could use your help.

Desktop.getDesktop().open(file); // Java version
svstackoverflow
  • 665
  • 6
  • 19
  • 1
    The corresponding operation is to start an activity with an `ACTION_VIEW` `Intent`. The [documentation on common Intents](https://developer.android.com/guide/components/intents-common?hl=en#PlayMedia) is focused a bit on media playback, but the same structure applies for other file types. Just bear in mind that there is no "if none is associated or exists" in Android -- if the device does not have an app that supports the desired file type, your `startActivity()` call will throw an `ActivityNotFoundException`. – CommonsWare Mar 12 '20 at 18:01
  • @CommonsWare: Thanks for explaining and taking the time. It sounds as if I need to further understand how to use Intents and accommodating for possible exceptions. So far, all I have used Intents for are for launching an activity and passing a parameter if needed to the activity. – svstackoverflow Mar 12 '20 at 19:05

1 Answers1

1

I did resolve my issue based upon CommonWare's suggestion of using an Intent (see above). However, I did have a number of Uri path issues and needed to include the use of a FileProvider to handle the internal file path when building the Uri used in the Intent.

I provided code which I used to build out my Uri path which was passed to the Intent (wrapped in the openFile(Uri uri) method. If it helps to see the full context of how I used the Intent, you can see it here: (Why would an Android Open File intent hang while opening an image?).

The openFile() handling the actual Intent:

private void openFile(Uri uri){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(intent, 2);
}

The TableRow onClick() creating the Uri from a TableRow value The Uri is built through the FileProvider beginning with File class calls.

row.setOnClickListener(v -> {

    TableRow tablerow = (TableRow) v;
    TextView sample = (TextView) tablerow.getChildAt(1);
    String result = sample.getText().toString();

    File filePaths = new File(getFilesDir().toString());
    File newFile = new File(filePaths, result);
    Uri contentUri = getUriForFile(getApplicationContext(), "com.mydomain.fileprovider", newFile);
    openFile(contentUri);

});
duplode
  • 33,731
  • 7
  • 79
  • 150
svstackoverflow
  • 665
  • 6
  • 19