Firemonkey Android InputStream reads 0s for files larger than 1MB

  

My Firemonkey Android app launches for multiple file types including zip files. When I check the intent and try to read the file that launched my app, I read 0s for files over 1MB. I don’t know the exact size limits. A 1.7KB file reads correctly, a 1.36MB file does not. It reads the correct number of bytes, and after reading 0s for about 2/3s of the file, it finally starts reading actual file data from the beginning of the file. This is my first attempt at such things. I created simplified test code to explore the problem:
function StartupFileGet : string;
var
intent: JIntent;
uri: Jnet_Uri;
uriStr: String;
InStrm: Androidapi.Jni.Javatypes.JInputStream;
ch: Char;
total: integer;
begin
result := ”;
intent := TAndroidHelper.Activity.getIntent;
if intent <> nil then
begin
if TJIntent.JavaClass.ACTION_VIEW.equals(intent.getAction) then
begin
uri := intent.getData;
uriStr := JStringToString(uri.toString);
result := uriStr;
result := result + ‘=’;
InStrm := TAndroidHelper.Activity.getContentResolver().openInputStream(uri);
if InStrm <> nil then
begin
total := InStrm.available;
if total > 0 then
begin
try
ch := Char(InStrm.read());
if ch = #0 then ch := #$30;
result := result + ch;
ch := Char(InStrm.read());
if ch = #0 then ch := #$30;
result := result + ch;
except
result := result + ‘, exception’;
end;
result := result + ‘, ‘;
result := result + IntToStr(total);
end;
end;
InStrm.close;
end;
end;
end;

Incorrect result with a 1.36MB zip file is:
Startup file: content://com.google.android.apps.nbu.files.provider/2/1044=00, 1362165
Correct result with a 1.7KB zip file is:
Startup file: content://com.google.android.apps.nbu.files.provider/2/1045=PK, 1697
In both the above results, the file sizes are correct. Android 9, both files are in the downloads folder. Rad Studio 10.4.1

Comments are closed.