0

I have an app that use its own file format.

I want that when the user click on a file with extension .myAppFormatfrom any file manager

I have already read this question to understand how to register a new file format.

My problem: How to get the full file path on opening to allow my method to load the file?

My method is something like:

public void loadFile(String path){
FileInputStream savedSerializable = new FileInputStream(path);
...
}
Community
  • 1
  • 1
AndreaF
  • 11,975
  • 27
  • 102
  • 168

1 Answers1

1

If your activity is newly created, in onCreate() you can call getIntent().getData() to get the Uri of the file that triggered your activity.

If your activity instance already existed, call getData() on the Intent that is delivered to your onNewIntent() method.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • How exactly should I manage these 2 different situations? My Activivity could exist or could be closed if the user hasn't started the app – AndreaF May 12 '14 at 19:29
  • @AndreaF: I have no way of answering that, as I am not the one writing your app. I would expect that many apps would use `onCreate()` to set up and populate the UI, and use `onNewIntent()` to populate the existing UI. – CommonsWare May 12 '14 at 19:30
  • Yes I use `onCreate`e also in all my `Activity`... but assuming that I'm in activity `B` where `B` isn't the main Activity... how could listen if someone is trying to open an associated file, so that I can switch to the home `Activity` in the appropriate way? – AndreaF May 12 '14 at 19:34
  • @AndreaF: The activity that has the `` matching the `Intent` will come to the foreground if it already exists, automatically. – CommonsWare May 12 '14 at 19:36
  • ok... thank you! I will try your suggestion and eventually accept the answer if works. – AndreaF May 12 '14 at 20:34
  • get `java.lang.NullPointerException` if I try `getIntent().getData().getEncodedPath();` I have also tried to do `Intent i=i.getIntent() if(i!=null){string path=getData().getEncodedPath();` but get same issue – AndreaF May 12 '14 at 21:54
  • @AndreaF: Open up a fresh Stack Overflow question, showing your code and the complete stack trace. – CommonsWare May 12 '14 at 22:12
  • I have used a try catch to avoid the issue... if the app is already opened the solution works, otherwise hangs. details here http://stackoverflow.com/questions/23620337/if-i-try-to-register-a-file-associaton-the-app-hangs-at-the-start – AndreaF May 12 '14 at 23:03