File Paths in Android Enterprise

If I want to access an image or file that is contained in the system.ResourceDirectory is where themain.lua file is, from native android code, how do I do it?

For instance “file://imagename.png”? I just need the appropriate path as I am passing it to another Intent as an argument.

I am now aware that images cannot be directly used from the ResourceDirectory on android. Same question though for images in the DocumentsDirectory.

Hi,

I think what you want is located here: http://docs.coronalabs.com/native/android/html/com/ansca/corona/CoronaEnvironment.html

You can call CoronaEnvironment.getDocumentsDirectory().

If this intent is being sent to one of your own activities, then you can easily open an input stream to your file via Corona’s “FileServices” class.  This class provides access to files in the ResourceDirectory and DocumentsDirectory.  Files under what Corona calls the ResourceDirectory are referred to as “asset” files on Android because they’re typically within the APK’s “assets” directory… or the Google Play expansion file if you’re using it.
   http://docs.coronalabs.com/daily/native/android/html/com/ansca/corona/storage/FileServices.html

If you are sending an intent to another app, then you’ll run into a different problem. External apps do not have direct access to your app’s files. In this case, you’ll need to use Corona’s “FileContentProvider” class which provides read-only access to your app’s files. The way this works is that you create a URI that references your file by calling FileContentProvider.createContentUriForFile(…). When you pass this URI to an external app, it then access Corona’s FileContentProvider to get a hold of the file’s input stream and access its bytes. This works for both internal resource/asset files and external files in the documents directory.
   http://docs.coronalabs.com/daily/native/android/html/com/ansca/corona/storage/FileContentProvider.html

This is making more sense. I’m not really sure how to use FileContentProvider though.

My image is named share4.png and is located in the resource folder alongside main.lua. I am trying to share it to another application.

This:

                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FileContentProvider.createContentUriForFile(fParentActivity,“share4.png”)));

fParentActivity.startActivity(shareIntent);

gets this error:

cannot find symbol

    [javac] symbol: variable FileContentProvider

    [javac]                     shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FileContentProvider.createContentUriForFile(fParentActivity,“share4.png”)));

Nevermind, just had to import it and stop using Uri.parse.

import com.ansca.corona.storage.FileContentProvider;

 

shareIntent.putExtra(Intent.EXTRA_STREAM, FileContentProvider.createContentUriForFile(fParentActivity,“share4.png”));

fParentActivity.startActivity(shareIntent);

 

 

Thanks!

May I ask if you are making a sharing intent? (to share text, urls or images?) FYI: We support that via our social plugin on android now. :slight_smile:

It’s for Instagram. Do you support that? I don’t want an option pane with sharing options though, just straight to the app.

Thanks!

Ah i see. No sorry, that isn’t supported out of the box.

Best of luck!

I am now aware that images cannot be directly used from the ResourceDirectory on android. Same question though for images in the DocumentsDirectory.

Hi,

I think what you want is located here: http://docs.coronalabs.com/native/android/html/com/ansca/corona/CoronaEnvironment.html

You can call CoronaEnvironment.getDocumentsDirectory().

If this intent is being sent to one of your own activities, then you can easily open an input stream to your file via Corona’s “FileServices” class.  This class provides access to files in the ResourceDirectory and DocumentsDirectory.  Files under what Corona calls the ResourceDirectory are referred to as “asset” files on Android because they’re typically within the APK’s “assets” directory… or the Google Play expansion file if you’re using it.
   http://docs.coronalabs.com/daily/native/android/html/com/ansca/corona/storage/FileServices.html

If you are sending an intent to another app, then you’ll run into a different problem. External apps do not have direct access to your app’s files. In this case, you’ll need to use Corona’s “FileContentProvider” class which provides read-only access to your app’s files. The way this works is that you create a URI that references your file by calling FileContentProvider.createContentUriForFile(…). When you pass this URI to an external app, it then access Corona’s FileContentProvider to get a hold of the file’s input stream and access its bytes. This works for both internal resource/asset files and external files in the documents directory.
   http://docs.coronalabs.com/daily/native/android/html/com/ansca/corona/storage/FileContentProvider.html

This is making more sense. I’m not really sure how to use FileContentProvider though.

My image is named share4.png and is located in the resource folder alongside main.lua. I am trying to share it to another application.

This:

                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FileContentProvider.createContentUriForFile(fParentActivity,“share4.png”)));

fParentActivity.startActivity(shareIntent);

gets this error:

cannot find symbol

    [javac] symbol: variable FileContentProvider

    [javac]                     shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FileContentProvider.createContentUriForFile(fParentActivity,“share4.png”)));

Nevermind, just had to import it and stop using Uri.parse.

import com.ansca.corona.storage.FileContentProvider;

 

shareIntent.putExtra(Intent.EXTRA_STREAM, FileContentProvider.createContentUriForFile(fParentActivity,“share4.png”));

fParentActivity.startActivity(shareIntent);

 

 

Thanks!

May I ask if you are making a sharing intent? (to share text, urls or images?) FYI: We support that via our social plugin on android now. :slight_smile:

It’s for Instagram. Do you support that? I don’t want an option pane with sharing options though, just straight to the app.

Thanks!

Ah i see. No sorry, that isn’t supported out of the box.

Best of luck!