SDL2 on Android get "Open with" arguments

Hello everyone!

I have an SDL 2.0 application that will load a file provided by another application (like Dropbox for instance) thanks to “Open with” dialog.

But I don’t know how retrieve the argument given to my app from the other application. On iOS, for the exact same problem, I had to use SDL_DROPFILE event, but it doesn’t seem to work the same way for Android.

I looked at SDLActivity.java to find some clue, but I didn’t find anything.

Do you know how to do that?

Thanks in advance [Wink]

Hi Noxalus,

you have to use Android Intents for this. In the Android port of the
L?VE engine we can open games by opening a link or a file on the phone.

In the AndroidManifest.xml you have to define intent-filters for your
activity that take into account the file scheme, mime type etc. It can
be a bit tricky to get those right and I am still not yet happy how we
do it.

You can look at https://bitbucket.org/MartinFelis/love-android-sdl2
which might help you get started. The relevant files are
AndroidManifest.xml and src/love2d/android/GameActivity.java.

Good luck!
MartinOn 22.10.2014 10:31, Noxalus wrote:

Hello everyone!

I have an SDL 2.0 application that will load a file provided by another
application (like Dropbox for instance) thanks to “Open with” dialog.

But I don’t know how retrieve the argument given to my app from the
other application. On iOS, for the exact same problem, I had to use
SDL_DROPFILE event, but it doesn’t seem to work the same way for Android.

I looked at SDLActivity.java to find some clue, but I didn’t find anything.

Do you know how to do that?

Thanks in advance Wink


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks a lot Martin for you answer! :smiley:

I just saw that we can effectively get the filename from intent on this post: http://stackoverflow.com/questions/2971871/how-to-get-the-file-name-from-the-intent?answertab=active#tab-top

I can do like you, but the best way would be to let SDL manage this instead.

For iOS, if we look at the file SDL_uikitappdelegate.m, we can see (line 275) that it uses SDL_SendDropFile function to trigger an SDL_DropEvent with the filename. It should be possible to do the exact same thing updating SDLActivity.java, isn’t it ?

Ok, I performed some changes to SDL files and I can now retrieve the filename from SDL_DropEvent like on iOS.

What I changed is very simple. For SDLActivity.java, I added this code at the end of onCreate method (line 99):

Code:

// Get filename from “Open with” of another application
Intent intent = getIntent();

if (intent != null && intent.getData() != null)
{
String filename = intent.getData().getPath();
Log.v(“SDL”, “Get filename:” + filename);

SDLActivity.onNativeDropFile(filename);

}

Without forget to declare onNativeDropFile among the C functions (line 284):

Code:
public static native void onNativeDropFile(String filename);

To finish, I updated the file SDL_android.c (into src/core/android) to add the onNativeDropFile function (line 143):

Code:

// Drop file
void Java_org_libsdl_app_SDLActivity_onNativeDropFile(
JNIEnv* env, jclass jcls,
jstring filename)
{
const char *path = (*env)->GetStringUTFChars(env, filename, NULL);

SDL_SendDropFile(path);

(*env)->ReleaseStringUTFChars(env, filename, path);

}

I would like to suggest a pull request on the SDL repository, but they use Mercurial and I don’t know if it’s possible… :frowning:

2014-10-22 14:40 GMT+02:00 Noxalus :

Ok, I performed some changes to SDL files and I can now retrieve the
filename from SDL_DropEvent like on iOS.

What I changed is very simple. For SDLActivity.java, I added this code at
the end of onCreate method (line 99):

Code:

// Get filename from “Open with” of another application
Intent intent = getIntent();

if (intent != null && intent.getData() != null)
{
String filename = intent.getData().getPath();
Log.v(“SDL”, “Get filename:” + filename);

SDLActivity.onNativeDropFile(filename);
}

Without forget to declare onNativeDropFile among the C functions (line 284
):

Code:

public static native void onNativeDropFile(String filename);

To finish, I updated the file SDL_android.c (into src/core/android) to
add the onNativeDropFile function (line 143):

Code:

// Drop file
void Java_org_libsdl_app_SDLActivity_onNativeDropFile(
JNIEnv* env, jclass jcls,
jstring filename)
{
const char *path = (*env)->GetStringUTFChars(env, filename, NULL);

SDL_SendDropFile(path);

(*env)->ReleaseStringUTFChars(env, filename, path);

}

I would like to suggest a pull request on the SDL repository, but they use
Mercurial and I don’t know if it’s possible… [image: Sad]

You can create a patch and attach it in a bugzilla ticket:
https://bugzilla.libsdl.org/enter_bug.cgi?product=SDL
Then it will (hopefully) not be forgotten.

I don’t know what you mean by “patch”, but I posted the “bug” here: https://bugzilla.libsdl.org/show_bug.cgi?id=2762

I just put the updated files as attachment. Is it the only way to contribute? Github and its pull request system is far more convenient!

2014-10-22 15:48 GMT+02:00 Noxalus :

I don’t know what you mean by “patch”, but I posted the “bug” here:
https://bugzilla.libsdl.org/show_bug.cgi?id=2762

I just put the updated files as attachment. Is it the only way to
contribute? Github and its pull request system is far more convenient!

(Suddenly I know how old people feel…)

Assuming you cloned the mercurial repo:
hg diff > my_fix.patch

Then attach ‘my_fix.patch’ in bugzilla.

Thanks for your help Jonas, I replaced the former attachment by a real patch.