Hi Erel,
I have kept trying with this, and am still not able to get the file from the Action.View intent that is triggered from the Gmail app. I have found an example of what I am trying to do, but am unfamiliar with the language it is in/how I could convert it to b4a.
The example is here:
http://stackoverflow.com/questions/17388756/how-to-access-gmail-attachment-data-in-my-app
The manifest code seems to match mine, with the exception of the mimetype. Theirs is set as "application/octet-stream", which failed to catch the Action.View intent when I inserted it into mine (I use text/plain instead, which does catch the intent). I have also added in the "Browsable" category to match theirs, although I don't really understand the significance... (it doesn't seem to have had any impact)
I can follow their onRestart() code for the following section:
Intent intent = getIntent();
InputStream
is = null;
FileOutputStream
os = null;
String fullPath = null;
try {
String action = intent.getAction();
if (!Intent.ACTION_VIEW.equals(action))
return;
as this seems to be just defining variables and calling Activity.GetStartingIntent, and then confirming that the intent that has been captured is an Action.View. However, after this is where I am running into difficulties.
Uri uri = intent.getData();
String scheme = uri.getScheme();
String name = null;
if (scheme.equals("file")) {
List<String>
pathSegments = uri.getPathSegments();
if (pathSegments.size() > 0)
name = pathSegments.get(pathSegments.size() - 1);
}
else if (scheme.equals("content")) {
Cursor cursor = getContentResolver().query(
uri,
new String[]{MediaStore.MediaColumns.DISPLAY_NAME},
null,
null,
null);
cursor.moveToFirst();
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex >= 0)
name = cursor.getString(nameIndex);
}
else
return;
if (name == null)
return;
int n = name.lastIndexOf(".");
String fileName,
fileExt;
if (n == -1)
return;
else {
fileName = name.substring(0, n);
fileExt = name.substring(n);
if (!fileExt.equals(".gcsb"))
return;
}
fullPath = /*create full path to where the file is to go, including name/ext*/;
The code seems to be saving the Uri as a specific Uri object (which I haven't been able to find in B4A... Does it exist, or is there an equivalent? I think this may be the key, as everything after relies upon this reference to the file), and then using the Uri object to check the scheme (file or content). It then uses the scheme to determine how to build the destination filename; a file scheme loads the path segments into a list and gets the final segment, and a content scheme uses a ContentResolver (I assume this is some kind of string manipulator?) The next section then strips out the extension and ensures that the file is the correct format (.gcsb, in this case), and then specifies the Local destination.
I believe the scheme I am getting is Content, due to the Prefix of the Uri that is being returned from TextInput.GetData:
GetData = content://gmail-ls/
benji.fergy@gmail.com/messages/1123/attachments/0.1/BEST/false
although I haven't found any equivalent for uri.GetScheme, as scheme is listed as NOT CACHED in my intent (see attached image).
This means I will probably need to make use of something similar to the ContentResolver (if I understood what it is/how it is working).
The final section (apart from error trapping) then builds the new file (is it going character by character?), from the Uri object via an input stream to the file via an output stream.
is = getContentResolver().openInputStream(uri);
os = new FileOutputStream(fullPath);
byte[] buffer = new byte[4096];
int count;
while ((count = is.read(buffer)) > 0)
os.write(buffer, 0, count);
os.close();
is.close();
}
I believe I follow the logic of the coding, but I don't know if it is possible to translate it into B4A... any thoughts?
Cheers, and thanks for all your help so far.
Ben