Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: CBXE7 - Launching a file programmatically within Android application.


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Sep 11, 2014 9:21 AM Last Post By: Remy Lebeau (Te...
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
CBXE7 - Launching a file programmatically within Android application.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 10, 2014 8:50 PM
I have the following sample code to run a file programmatically fwithin an Android application:

_di_JIntent oIntent;
_di_Jnet_Uri uri;

UnicodeString us = "file:///mnt/sdcard2/demo/readonly/test.txt";

uri = StrToJURI( us );

oIntent = TJIntent::JavaClass->init( TJIntent::JavaClass->ACTION_VIEW, uri );
if( oIntent )
{
::SharedActivityContext()->startActivity( oIntent );
}

However, I received the following error message:

android.content.ActivitiyNotFoundException: No Activity found to handle Intent {act=android.intent.action.VIEW dat=file:///mnt/sdcard2/demo/readonly/test.txt}

Please advise.

GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: CBXE7 - Launching a file programmatically within Android application.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2014 2:38 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
...just stranded around to find solutions through the web and eventually pick up puzzle piece by piece and
here is the final answer.

_di_JMimeTypeMap oMimeTypeMap;
_di_JIntent oIntent;
_di_Jnet_Uri uri;

UnicodeString us = "file:///mnt/sdcard2/demo/readonly/test.txt";

uri = StrToJURI( us );

oIntent = TJIntent::JavaClass->init();
if( oIntent )
{
oIntent->setAction( TJIntent::JavaClass->ACTION_VIEW );

TIdMimeTable* oMime = new TIdMimeTable( true );

oIntent->setDataAndType( uri, StringToJString( oMime->GetFileMIMEType( us ) ) ); // GetFileMIMEType() returns "text/plain"

::SharedActivityContext()->startActivity( oIntent );
}

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CBXE7 - Launching a file programmatically within Androidapplication.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2014 9:21 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

here is the final answer.

Try using Android's own MimeTypeMap class instead of Indy's TIdMimeTable
class:

UnicodeString us = L"file:///mnt/sdcard2/demo/readonly/test.txt";
_di_Jnet_Uri uri = StrToJURI( us );
 
_di_JIntent oIntent = TJIntent::JavaClass->init();
if( oIntent )
{
    oIntent->setAction( TJIntent::JavaClass->ACTION_VIEW );
 
    _di_JString MimeType = TJMimeTypeMap::JavaClass->getSingleton()->getMimeTypeFromExtension( 
StringToJString(L"txt") );
    oIntent->setDataAndType( uri, MimeType );
 
    ::SharedActivityContext()->startActivity( oIntent );
}


If you want to use TIdMimeTable, then you should at least use Indy's GetMIMETypeFromFile()
function instead, you do not need to instantiate TIdMimeTable directly:

oIntent->setDataAndType( uri, StringToJString( GetMIMETypeFromFile(us) ) );


--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CBXE7 - Launching a file programmatically within Android application.[Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2014 9:15 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

::SharedActivityContext()->startActivity( oIntent );

However, I received the following error message:

+android.content.ActivitiyNotFoundException: No Activity found to
handle Intent {act=android.intent.action.VIEW
dat=file:///mnt/sdcard2/demo/readonly/test.txt}+

It means Android could not find an application to handle the content of your
Intent. In this case, no application that knows how to view a .txt file.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02