无法打开 Android 应用程序创建的 pdf 文件
Cannot open pdf file created by Android app
我正在使用 delphi xe6 创建 Android 应用程序。
Android 应用正在从 Web 服务服务器下载 pdf 文件作为字节数组。通过使用 TFileStream,创建 pdf 文件并写入缓冲区(字节数组)。
但是,问题是未创建 pdf 文件。当尝试打开文件时出现错误,就像没有文件一样。
如果我开发与桌面应用程序相同的文件,则可以正确创建 pdf 文件。
这是代码片段。
FileStream := TFileStream.Create(TPath.GetDocumentsPath + PathDelim + 'Sample.pdf', fmCreate or fmOpenWrite);
try
FileResponse := GetIFileTransfer.GetFile; //GetIFileTransfer is Webservice object
FileStream.WriteBuffer(FileResponse.Bytes[0], Length(FileResponse.Bytes));
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(StrToJURI(TPath.GetDocumentsPath + PathDelim + 'Sample.pdf'),
StringToJString('application/pdf'));
SharedActivity.startActivity(Intent);
finally
FileStream.Free;
end;
感谢您的回答。我找到了解决方案。这里是更改后的代码片段
FileStream := FileStream.Create(TPath.Combine(TPath.GetSharedDocumentsPath, 'sample.pdf'), fmCreate or fmOpenWrite);
try
FileResponse := GetIFileTransfer.GetFile;
FileStream.WriteBuffer(FileResponse.Bytes[0], Length(FileResponse.Bytes));
finally
FileStream.Free;
end;
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(StrToJURI('file://' + TPath.Combine(TPath.GetSharedDocumentsPath, 'Sample.pdf')),
StringToJString('application/pdf'));
SharedActivity.startActivity(Intent);
我正在使用 delphi xe6 创建 Android 应用程序。 Android 应用正在从 Web 服务服务器下载 pdf 文件作为字节数组。通过使用 TFileStream,创建 pdf 文件并写入缓冲区(字节数组)。
但是,问题是未创建 pdf 文件。当尝试打开文件时出现错误,就像没有文件一样。
如果我开发与桌面应用程序相同的文件,则可以正确创建 pdf 文件。
这是代码片段。
FileStream := TFileStream.Create(TPath.GetDocumentsPath + PathDelim + 'Sample.pdf', fmCreate or fmOpenWrite); try FileResponse := GetIFileTransfer.GetFile; //GetIFileTransfer is Webservice object FileStream.WriteBuffer(FileResponse.Bytes[0], Length(FileResponse.Bytes)); Intent := TJIntent.Create; Intent.setAction(TJIntent.JavaClass.ACTION_VIEW); Intent.setDataAndType(StrToJURI(TPath.GetDocumentsPath + PathDelim + 'Sample.pdf'), StringToJString('application/pdf')); SharedActivity.startActivity(Intent); finally FileStream.Free; end;
感谢您的回答。我找到了解决方案。这里是更改后的代码片段
FileStream := FileStream.Create(TPath.Combine(TPath.GetSharedDocumentsPath, 'sample.pdf'), fmCreate or fmOpenWrite); try FileResponse := GetIFileTransfer.GetFile; FileStream.WriteBuffer(FileResponse.Bytes[0], Length(FileResponse.Bytes)); finally FileStream.Free; end; Intent := TJIntent.Create; Intent.setAction(TJIntent.JavaClass.ACTION_VIEW); Intent.setDataAndType(StrToJURI('file://' + TPath.Combine(TPath.GetSharedDocumentsPath, 'Sample.pdf')), StringToJString('application/pdf')); SharedActivity.startActivity(Intent);