Watch, Follow, &
Connect with Us

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


Welcome, Guest
Guest Settings
Help

Thread: CBXE7 - JStringToString, StringToJString, StrToJURI not in HELP document ?


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


Permlink Replies: 2 - Last Post: Sep 12, 2014 1:08 AM Last Post By: GAI CHEW KAI
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
CBXE7 - JStringToString, StringToJString, StrToJURI not in HELP document ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2014 2:51 AM
Somehow, I have spent hours while coding CBXE7 for Android mobile just to find translation from Android Java side to C++Builder XE,
for example java code below:

String ext = MimeTypeMap.getFileExtensionFromUrl( fileUri.toString() );

1. How do you declare MimeTypeMap object in CBXE ?

- What I did was:
- manually goto command prompt and types:

C:\Program Files (x86)\Embarcadero\Studio\15.0\include\android\rtl>grep "MimeTypeMap" .

- only to find "_di_JMimeTypeMap" was in Androidapi.JNI.Webkit.hpp

- so, put it into source as #include <Androidapi.JNI.Webkit.hpp>

- declare it as: _di_JMimeTypeMap oMimeTypeMap;

2. Now, how do you create object of _di_JMimeTypeMap and assign to oMimeTypeMap ?

- oMimeTypeMap = TJMimeTypeMap::Create(); // where there is TJMimeTypeMap::JavaClass->init(); which cause memory access error when accessing its method, not sure why ?

- _di_JString js = oMimeTypeMap->getMimeTypeFromExtension( StringToJString( us ) ); // It seems there is no .getFileExtensionFromUrl(), variable us is based on "UnicodeString us" stated above.

- TLabel1->Text = JStringToString( js ); // TLabel1->Text is empty, this I am not sure why ? even if I use getMimeTypeFromExtension( StringToJString( ".txt" ) );

3. To know the return type of certain functions, I have to check the header file from command line also:

- C:\Program Files (x86)\Embarcadero\Studio\15.0\include\android\rtl>grep "JStringToString" .
Androidapi.Helpers.hpp
extern DELPHI_PACKAGE System::UnicodeString __fastcall JStringToString(const Androidapi::Jni::Javatypes::_di_JString JStr);

- as you can see, the return type is UnicodeString above.

I am not sure whether I am doing the right thing or not with the above mentioned approach, because "F1" just won't find it in HELP document.

If there is documentation to differential ::JavaClass->Init() and ::Create(), please do give guidelines here and very much appreciated.

Please advise.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CBXE7 - JStringToString, StringToJString, StrToJURI not in HELPdocument ? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2014 9:09 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

String ext = MimeTypeMap.getFileExtensionFromUrl( fileUri.toString() );

1. How do you declare MimeTypeMap object in CBXE ?

getFileExtensionFromUrl() is a static method:

http://developer.android.com/reference/android/webkit/MimeTypeMap.html#getFileExtensionFromUrl(java.lang.String)

So you don't need an object. Use the TJMimeTypeMap::JavaClass property to
access the underlying Android MimeTypeMap class, then call the method like
you would any other static method, eg:

#include <Androidapi.JNI.Webkit.hpp>
 
String ext = JStringToString( TJMimeTypeMap::JavaClass->getFileExtensionFromUrl( 
fileUri->toString() ) );


2. Now, how do you create object of _di_JMimeTypeMap and assign to
oMimeTypeMap ?

MimeTypeMap is implemented as a singleton class, so you have to call its
getSingleton() method when you need an object instance:

http://developer.android.com/reference/android/webkit/MimeTypeMap.html#getSingleton()

For example:

_di_JMimeTypeMap oMimeTypeMap = TJMimeTypeMap::JavaClass->getSingleton();
_di_JString js = oMimeTypeMap->getMimeTypeFromExtension( StringToJString( 
us ) );
String sMimeType = JStringToString(js);


there is TJMimeTypeMap::JavaClass->init(); which cause memory access
error when accessing its method, not sure why ?

The MimeTypeMap constructor is private in Android, you cannot call it directly.

It seems there is no .getFileExtensionFromUrl()

Yes, there is, just not where you are expecting it. getFileExtensionFromUrl()
is a static method, not an instance method, so it exists in the TJMimeTypeMap
wrapper class, not in the _di_JMimeTypeMap wrapper interface.

A TJ... wrapper class represents a Java class type, its constructor(s), and
static members only.

A _di_J... wrapper interface represents an object instance, and is used to
access the object's non-static members only.

3. To know the return type of certain functions, I have to check the
header file from command line also:

Yes, or refer to the Android documentation. Android class type names have
a TJ prefix added in C++/Delphi wrappers. Objects instances are accessed
via interfaces that have a J prefix, and in C++ those interfaces are wrapped
with the DelphiInterface class (hense the di prefix). Thus, for the Android
MimeTypeMap class, you have the TJMimeTypeMap and (_di_)JMimeTypeMap wrappers.

--
Remy Lebeau (TeamB)
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: CBXE7 - JStringToString, StringToJString, StrToJURI not in HELPdocument ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 12, 2014 12:22 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
ok, now tested and working solution is here below finally :o)

_di_JMimeTypeMap oMimeTypeMap;
_di_JString jsMimeType;
_di_JString jsExt;

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

// StrToJURI( UnicodeString )
uri = StrToJURI( us );

oMimeTypeMap = TJMimeTypeMap::JavaClass->getSingleton();

// StringToJString( UnicodeString );
// getFileExtensionFromUrl() returns as "txt", so jsExt is "txt"
jsExt = TJMimeTypeMap::JavaClass->getFileExtensionFromUrl( StringToJString( us ) );

// getMimeTypeFromExtension() returns "text/plain", so jsMimeType is "text/plain"
jsMimeType = oMimeTypeMap->getMimeTypeFromExtension( jsExt );

Hopes, this gives a clear picture to everyone.

Thanks again for the help, Remy.

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02