//handling file by uri....
function GetBitmapFromUri
function GetTextFromUri
procedure SaveImageToUri
procedure SaveTextToUri
New demo:
"AppPublicFoldersAccessDemo1"
I have added function
SaveBytesToUri.
The updated code is in attachment, in two files:
1) jForm.java, located at
C:\fpcupdeluxe\ccr\lamw\android_wizard\smartdesigner\java
2) androidwidget.pas, located at
C:\fpcupdeluxe\ccr\lamw\android_bridges
jmpessoa, please search the attached files for phrase “by neuro” and copy/paste new code into LAMW distribution files.
The new code in “androidwidget.pas”:
procedure SaveBytesToUri(_bytes: TDynArrayOfJByte; _toTreeUri: jObject); // by neuro
procedure jForm_SaveBytesToUri(env: PJNIEnv; _jform: JObject; _bytes: TDynArrayOfJByte; _toUri: jObject); // by neuro
//by neuro
procedure jForm.SaveBytesToUri(_bytes: TDynArrayOfJByte; _toTreeUri: jObject);
begin
//in designing component state: set value here...
if FInitialized then
jForm_SaveBytesToUri(FjEnv, FjObject, _bytes ,_toTreeUri);
end;
//by neuro
procedure jForm_SaveBytesToUri(env: PJNIEnv; _jform: JObject; _bytes: TDynArrayOfJByte; _toUri: jObject);
var
jParams: array[0..1] of jValue;
jMethod: jMethodID=nil;
jCls: jClass=nil;
byteArray: jByteArray;
len: SizeInt;
label
_exceptionOcurred;
begin
jCls:= env^.GetObjectClass(env, _jform);
if jCls = nil then goto _exceptionOcurred;
jMethod:= env^.GetMethodID(env, jCls, 'SaveBytesToUri', '([BLandroid/net/Uri;)V');
if jMethod = nil then goto _exceptionOcurred;
len := Length(_bytes);
byteArray:= env^.NewByteArray(env, len);
env^.SetByteArrayRegion(env, byteArray, 0 , len, @_bytes[0]);
jParams[0].l:= byteArray;
jParams[1].l:= _toUri;
env^.CallVoidMethodA(env, _jform, jMethod, @jParams);
env^.DeleteLocalRef(env,jParams[0].l);
env^.DeleteLocalRef(env, jCls);
_exceptionOcurred: jni_ExceptionOccurred(env);
end;
The new code in “jForm.java”:
//Android 11: //by neuro
public void SaveBytesToUri(byte[] _bytes, Uri _toTreeUri) {
try {
out = controls.activity.getContentResolver().openOutputStream(_toTreeUri, "w");
out.write(_bytes);
out.flush();
e.printStackTrace();
e.printStackTrace();
}
}
Usage example:
var
array_of_bytes: array of byte;
array_of_jbytes: TDynArrayOfJByte;
n, size_of_array: Integer;
begin
size_of_array := 256;
setLength(array_of_bytes, size_of_array);
setLength(array_of_jbytes, size_of_array);
for n := 0 to size_of_array-1 do
begin
array_of_bytes[n] := Byte(128); // here set the values of byte array
array_of_jbytes[n] := JByte(array_of_bytes[n]);
end;
Self.SaveBytesToUri(array_of_jbytes, treeUri);
end;
We also need function
GetBytesFromUriwhich is not yet available.
In sample project AppPublicFoldersAccessDemo1
two buttons work (“Request Create File” and “Request Open Directory”),
however third button “Request Open File” does not work – nothing happens when this button is pressed.
I guess this button should invoke system file picker (equivalent of open file dialog), however it does not work.
I am running AppPublicFoldersAccessDemo1 on Android 8.1 (device: Samsung Galaxy Tab A 10.1).
I have fixed this bug in the post below.