Delphi 113 andriod12 接收共享文件
要在 Delphi 11.3 上接收共享文件,您需要实现以下步骤:
- 在您的 Android 应用程序清单文件中添加以下权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
-
在您的 Delphi 11.3 应用程序中创建一个 TAndroidIntent 对象,并为其指定 ACTION_SEND_MULTIPLE 操作。
-
检查 Intent 对象中是否包含 EXTRA_STREAM 和 MIME_TYPE 数据。
-
将 EXTRA_STREAM 数据保存为文件并将其路径存储在变量中。
-
您可以使用 TPath.GetSharedDocumentsPath 函数来获取共享文档文件夹的路径。
-
将接收到的文件移动到共享文档文件夹中。
以下是一个示例代码:
uses
Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Net, Androidapi.JNI.JavaTypes,
System.IOUtils;
procedure TForm1.btnReceiveFileClick(Sender: TObject);
var
Intent: JIntent;
Uri: Jnet_Uri;
InputStream: JInputStream;
OutputStream: JOutputStream;
Bytes: TJavaArray<Byte>;
FilePath, FileName, SharedFolderPath, SharedFilePath: string;
begin
Intent := SharedActivity.Intent;
if Intent <> nil then
begin
if Intent.getAction.equals(TJIntent.JavaClass.ACTION_SEND_MULTIPLE) then
begin
if Intent.hasExtra(TJIntent.JavaClass.EXTRA_STREAM) and Intent.hasExtra(TJIntent.JavaClass.EXTRA_MIME_TYPES) then
begin
Uri := Intent.getParcelableExtra(TJIntent.JavaClass.EXTRA_STREAM);
InputStream := SharedActivity.getContentResolver.openInputStream(Uri);
SetLength(Bytes, InputStream.available);
InputStream.read(Bytes);
FilePath := TPath.GetDocumentsPath;
FileName := TPath.GetFileName(Uri.toString);
SharedFolderPath := TPath.GetSharedDocumentsPath;
SharedFilePath := TPath.Combine(SharedFolderPath, FileName);
OutputStream := TJFileOutputStream.JavaClass.init(StringToJString(SharedFilePath));
OutputStream.write(Bytes);
OutputStream.close;
ShowMessageFmt('File received and saved to %s', [SharedFilePath]);
end;
end;
end;
end;
请注意,此示例仅处理接收单个文件的情况。如果您要接收多个文件,请在代码中进行适当更改
原文地址: https://www.cveoy.top/t/topic/feeK 著作权归作者所有。请勿转载和采集!