Download and open PDF file (XE7/Android) [Edit]

Hi,

With the code below I try to download and open a pdf file on Android.

   {code}
    sPath := TPath.Combine(TPath.GetDocumentsPath, 'XXX.pdf');
    DeleteFile(sPath);
    MemoryStream := TMemoryStream.Create;
    IdHTTP1.Get('http://XXX.com/files/XXX.pdf', MemoryStream);
    MemoryStream.SaveToFile(sPath);

    if FileExists(sPath) then
    begin

      Intent := TJIntent.Create;
      Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
      Intent.setDataAndType(StrToJURI(sPath), StringToJString('application/pdf'));
      SharedActivity.StartActivity(Intent);

    end;
  {code}

FileExists return true so I assume that the file is correctly downloaded. 
But after the last command (StartActivity), I have the "File or folder not found" notification on android device.

If someone could help me....

Thanks in advance ;)

Ben

Edited by: Benjamin Cabot on Nov 27, 2014 1:55 AM
0
Benjamin
11/27/2014 9:55:41 AM
embarcadero.delphi.firemonkey 4901 articles. 4 followers. Follow

9 Replies
3880 Views

Similar Articles

[PageSpeed] 0

> {quote:title=Benjamin Cabot wrote:}{quote}
> Hi,
> 
> With the code below I try to download and open a pdf file on Android.
> 
>    {code}
>     sPath := TPath.Combine(TPath.GetDocumentsPath, 'XXX.pdf');
>   {code}
> 

You might try to use:
{code}
     sPath := TPath.Combine(TPath.GetSHAREDDocumentsPath, 'XXX.pdf');

{code}

Android needs the file to be in external storage in order to share it. You can't share a file in GetDocumentsPath, it must be GetSHAREDDocumentsPath.
And in order to access the external storage, you need to add permissions for external storage in the Application Properties.

Edited by: Adrian Gallero on Nov 27, 2014 2:25 AM
0
Adrian
11/27/2014 10:26:52 AM
{quote:title=Adrian Gallero wrote:}
 
 Android needs the file to be in external storage in order to share it. You can't share a file in GetDocumentsPath, it must be GetSHAREDDocumentsPath.
 And in order to access the external storage, you need to add permissions for external storage in the Application Properties.

{quote}

Thank you for your help, but it is the same with GetDocumentsPath.

And as you can see above, permissions were done.

    {code}<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />{code}
0
Benjamin
11/27/2014 11:37:31 AM
> {quote:title=Benjamin Cabot wrote:}{quote}
> {quote:title=Adrian Gallero wrote:}
>  
>  Android needs the file to be in external storage in order to share it. You can't share a file in GetDocumentsPath, it must be GetSHAREDDocumentsPath.
>  And in order to access the external storage, you need to add permissions for external storage in the Application Properties.
> 
> {quote}
> 
> Thank you for your help, but it is the same with GetDocumentsPath.
> 

Is GetDocumentsPath or Get*Shared*Documents path?

GetDocumentPath is a private folder for your app, and so nobody else can access it. SharedDocumentsPath is a folder that other can see. If SharedDocumentsPath isn't working, can you try any folder in the device that you know any app can access?  From your description, it seems that you have the permissions to write in the folder (that's why no errors on your side), but the pdf viewer doesn't have the permissions to see the file (that's why the error appears when you try to share the file)
0
Adrian
11/27/2014 1:53:53 PM
Of course it was GetSHAREDDocumentsPath and it did not work.

I'm not very familiar with Android. Do you have a suggestion for a free access directory that I can use?
0
Benjamin
11/27/2014 2:10:03 PM
> {quote:title=Benjamin Cabot wrote:}{quote}
> Of course it was GetSHAREDDocumentsPath and it did not work.
> 
> I'm not very familiar with Android. Do you have a suggestion for a free access directory that I can use?

It really depends in the device you are using. But anyway, looking at your code, I am not sure if that is the correct code. Can you try with something like this?  (I changed the indy component to a resourcestream just to make is simpler for me to test, but you can keep the indy component)

unit Unit4;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, IOUtils,
  Androidapi.JNI.Net, Androidapi.JNI.Os, Androidapi.Helpers,
  Androidapi.JNI.GraphicsContentViewText, AndroidAPI.JNI.JavaTypes;

type
  TForm4 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  Intent: JIntent;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.fmx}

procedure TForm4.Button1Click(Sender: TObject);
var
  sPath: string;
  MemoryStream: TResourceStream;

  fileuri: JParcelable;
begin
    sPath := TPath.Combine(TPath.GetSharedDocumentsPath, 'XXX.pdf');
    DeleteFile(sPath);
    MemoryStream := TResourceStream.Create(hinstance, 'xxx', RT_RCDATA);

    MemoryStream.SaveToFile(sPath);

    if FileExists(sPath) then
    begin
      fileuri := JParcelable(TJNet_Uri.JavaClass.fromFile(TJFile.JavaClass.init(StringToJString(sPath))));

      Intent := TJIntent.Create;
      Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
      intent.setType(StringToJString('application/pdf'));
      Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM, fileuri);
      SharedActivity.StartActivity(Intent);

    end;
end;

end.


Some notes in the code:
1)In my own, I use ACTION_SEND, not ACTION_VIEW
 It didn't  really work here with ACTION_VIEW (quickoffice stops working) but it might be something on my tablet. If ACTION_VIEW doesn't work for you either, you might try ACTION_SEND

2)The Intent is not a local variable but a global one. I don't remember the specifics, but I think that if you made it local, then the intent would be freed by arc when leaving the procedure, so it would crash. But I don't rememeber if this was for iOS or for android. In any case, I made it global just in case.
0
Adrian
11/27/2014 3:34:23 PM
Thank you so much for the time you spent for me!

So I tried the code below...

{code}
var
  sPath: string;
  MemoryStream: TMemoryStream;
  fileuri: JParcelable;
begin

  sPath := TPath.Combine(TPath.GetSharedDocumentsPath, 'xxx.pdf');

  DeleteFile(sPath);

  MemoryStream := TMemoryStream.Create;

  IdHTTP1.Get('http://xxx.com/xxx.pdf', MemoryStream);

  MemoryStream.SaveToFile(sPath);

  if FileExists(sPath) then
  begin

    fileuri := JParcelable(TJNet_Uri.JavaClass.fromFile(TJFile.JavaClass.init(StringToJString(sPath))));
    Intent := TJIntent.Create;
    // Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
    Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
    intent.setType(StringToJString('application/pdf'));
    Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM, fileuri);
    SharedActivity.StartActivity(Intent);

  end;
end;
{code}

....and I have the same issue than you (quickoffice crash at opening) with VIEW.
With SEND it opens the export dialog and I can send it by email, bluetooth,... successfully.

So now there is no issue for downloading the file and access it.
The issue is regarding the quickoffice opening...
0
Benjamin
11/28/2014 10:11:24 AM
> {quote:title=Benjamin Cabot wrote:}{quote}
> ...and I have the same issue than you (quickoffice crash at opening) with VIEW.
> With SEND it opens the export dialog and I can send it by email, bluetooth,... successfully.
> 
> So now there is no issue for downloading the file and access it.
> The issue is regarding the quickoffice opening...

Yes, that seems weird. From what I found, it looks like the call to SetType clears the data, and a call to setdata clears the type. So we need to set them both with SetDataAndType.

So I tried going back to your original code, and changing the url to start with file:// and now it seems to work:

Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
intent.setDataAndType(StrToJURI('file://' + spath) ,StringToJString('application/pdf'));
SharedActivity.StartActivity(Intent);


(Of course, I think there are some URI routines somewhere that will add the file://, but I didn't want to search for them)

So I think with the following 2 changes over the original code:
1)Change Documents by SharedDocuments
2)Add 'file://' to the uri

It should work. It works here, at least.
0
Adrian
11/28/2014 2:51:09 PM
El 28/11/14 a las 11:11, Benjamin Cabot escribió:
> {code}
> var
>    sPath: string;
>    MemoryStream: TMemoryStream;
>    fileuri: JParcelable;
> begin
>
>    sPath := TPath.Combine(TPath.GetSharedDocumentsPath, 'xxx.pdf');
>
>    DeleteFile(sPath);
>
>    MemoryStream := TMemoryStream.Create;
>
>    IdHTTP1.Get('http://xxx.com/xxx.pdf', MemoryStream);
>
>    MemoryStream.SaveToFile(sPath);
>
>    if FileExists(sPath) then
>    begin
>
>      fileuri := JParcelable(TJNet_Uri.JavaClass.fromFile(TJFile.JavaClass.init(StringToJString(sPath))));
>      Intent := TJIntent.Create;
>      // Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
>      Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
>      intent.setType(StringToJString('application/pdf'));
>      Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM, fileuri);
>      SharedActivity.StartActivity(Intent);
>
>    end;
> end;
> {code}
>

Edited by: Antonio Estevez on Nov 28, 2014 7:56 AM
0
Antonio
11/28/2014 3:56:53 PM
> {quote:title=Adrian Gallero wrote:}{quote}
> > {quote:title=Benjamin Cabot wrote:}{quote}
> > ...and I have the same issue than you (quickoffice crash at opening) with VIEW.
> > With SEND it opens the export dialog and I can send it by email, bluetooth,... successfully.
> > 
> > So now there is no issue for downloading the file and access it.
> > The issue is regarding the quickoffice opening...
> 
> Yes, that seems weird. From what I found, it looks like the call to SetType clears the data, and a call to setdata clears the type. So we need to set them both with SetDataAndType.
> 
> So I tried going back to your original code, and changing the url to start with file:// and now it seems to work:
> 
> Intent := TJIntent.Create;
> Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
> intent.setDataAndType(StrToJURI('file://' + spath) ,StringToJString('application/pdf'));
> SharedActivity.StartActivity(Intent);
> 
> 
> (Of course, I think there are some URI routines somewhere that will add the file://, but I didn't want to search for them)
> 
> So I think with the following 2 changes over the original code:
> 1)Change Documents by SharedDocuments
> 2)Add 'file://' to the uri
> 
> It should work. It works here, at least.

GREAT!!!! It works perfectly as it!
Thank you very much for your help Adrian.
0
Benjamin
11/28/2014 3:59:38 PM
Reply:

Similar Artilces:

Opening up a PAS file opens up another instance of Delphi XE IDE... [Edit]
Here's the problem: When I double click on a .Pas file. a new Delphi XE IDE opens up. Is there a way to open up the file inside of the Delphi XE IDE that is currently open? Edited by: Joseph Trapani on Jan 31, 2012 4:04 PM > Here's the problem: > When I double click on a .Pas file. a new Delphi XE IDE opens up. > Is there a way to open up the file inside of the Delphi XE IDE that is > currently open? > > Edited by: Joseph Trapani on Jan 31, 2012 4:04 PM Drag and drop the file onto the IDE? BTW: I have my .PAS files set to open in a text editor ...

How do I open/edit PDF files?
I have some pdf files on a usb stick which I want to edit, Evince cannot do this, and if I try to open in Libre there is no option to import pdf there either. Is there an app. or plugin for libre to allow this. Thanks -- jimt123 ------------------------------------------------------------------------ you can use 'PDF Studio' (http://www.qoppa.com/pdfstudio/). It's a commercial app. Or you can use pdfedit, pdfchain etc. Last 2 are free & opensource. -- AMD Phenom II X4 965 Black Edition, Gigabyte GA 880gm-ud2h mobo, 4GB RAM with Opensuse 11.4 K...

Downloads of pdf files do not open
Name: John Mactaggart Email: imactagatlineonedotnet Product: Firefox Summary: Downloads of pdf files do not open Comments: I have downloaded several pdf files and the open command in the download box does not work.You have to open the containg folder and open the file from there.Iam using Adobe reader 7.0 Browser Details: Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2 ...

Changing the default All Files (*.*) option to Adobe PDF Files (*.pdf) in a file open dialogue in ASP.net
In My file Upload page, I am using <input type=file> tag to browse a file from client machine.The Browse button by default is showing all files *.* in the file dialogue boxI have a requirement like the file dialogue should by default show just PDFs *.pdf, and have an option to show all files *.* as shown below:Adobe PDF Files (*.pdf)All Files (*.*)Is there a way to handle this using any web controls, or technique using ASP.net or HTML?Thanks in advanceAnsar I need answer to this one, too. As far as I know, there is no way to do this....

Cannot open the file .pdf when downLoad from server
 hi all The segment code below is used to download file from Server to client I cannot open the .pdf file when the file downloaded. But other files open normal. the message error as below: "There was an error openning this document. the file is damaged could not be repaired" But I open that file directly from server  is OK private bool DownloadFile(string serverFolder, string fileName){ bool result = false;string filePath = serverFolder + '\\' + fileName;if (System.IO.File.Exists(filePath)) { Response.Clear();Response.ContentType = "applica...

Download file from url and open in Android
Hello all: It is possible with Delphi XE5 for android, download a file (pdf) from a url and open it in a android device? How to do it? Thanks in advance > {quote:title=Alfonso Presa wrote:}{quote} > Hello all: > > It is possible with Delphi XE5 for android, download a file (pdf) from a url and open it in a android device? > > How to do it? > > Thanks in advance // ** Open a URL uses Androidapi.JNI.GraphicsContentViewText, FMX.Helpers.Android; procedure TForm3.Button1Click(Sender: TObject); var Intent: JIntent; begin I...

Open pdf file in iw [Edit]
Hello gentlemen someone could give me a hand need aquivo opened a PDF, which is called clientes.pdf in the location C: \ WebFacil \ WebFacil \ Win32 \ Debug \ Files \ clientes.pdf tried that did not work more: WebApplication.NewWindow (' C: \ WebFacil \ WebFacil \ Win32 \ Debug \ Files \ clientes.pdf ' ); what is wrong. Edited by: Benilton Ramos Santos on Apr 8, 2013 2:06 PM > WebApplication.NewWindow ('C: \ WebFacil \ WebFacil \ Win32 \ Debug \ Files \ clientes.pdf f'); > > what is wrong. i think removing the spaces could be a good start... :) ...

I Can Open But Not Download PDF Files
I can open PDF files in Seamonkey and when I try to download them the save file window opens but when I click on save nothing happens.? Thanks for your help, Chuck On 10/3/2015 8:16 PM, Chuck wrote: > I can open PDF files in Seamonkey and when I try to download them the > save file window opens but when I click on save nothing happens.? > Thanks for your help, > Chuck > Are you trying to save PDF files from a Web page link, from a link in an E-mail message, or from an attachment to an E-mail message? -- David E. Ross The Crimea is Putin's Sudetenland...

superreview requested: [Bug 243324] Download manager fails to close with files that download very quickly (small files / cached files) : [Attachment 164976] check closeWhenDone pref before opening do
Son Le <lesx99@lycos.com> has asked Ben Goodger <bugs@bengoodger.com> for superreview: Bug 243324: Download manager fails to close with files that download very quickly (small files / cached files) https://bugzilla.mozilla.org/show_bug.cgi?id=243324 Attachment 164976: check closeWhenDone pref before opening download window (v3) https://bugzilla.mozilla.org/attachment.cgi?id=164976&action=edit ...

superreview requested: [Bug 243324] Download manager fails to close with files that download very quickly (small files / cached files) : [Attachment 164720] check closeWhenDone pref before opening do
Son Le <lesx99@lycos.com> has asked Ben Goodger <bugs@bengoodger.com> for superreview: Bug 243324: Download manager fails to close with files that download very quickly (small files / cached files) https://bugzilla.mozilla.org/show_bug.cgi?id=243324 Attachment 164720: check closeWhenDone pref before opening download window (v2) https://bugzilla.mozilla.org/attachment.cgi?id=164720&action=edit ...

superreview cancelled: [Bug 243324] Download manager fails to close with files that download very quickly (small files / cached files) : [Attachment 164976] check closeWhenDone pref before opening do
Son Le <lesx99@lycos.com> has cancelled Son Le <lesx99@lycos.com>'s request for superreview: Bug 243324: Download manager fails to close with files that download very quickly (small files / cached files) https://bugzilla.mozilla.org/show_bug.cgi?id=243324 Attachment 164976: check closeWhenDone pref before opening download window (v3) https://bugzilla.mozilla.org/attachment.cgi?id=164976&action=edit ...

superreview cancelled: [Bug 243324] Download manager fails to close with files that download very quickly (small files / cached files) : [Attachment 164720] check closeWhenDone pref before opening do
Son Le <lesx99@lycos.com> has cancelled Son Le <lesx99@lycos.com>'s request for superreview: Bug 243324: Download manager fails to close with files that download very quickly (small files / cached files) https://bugzilla.mozilla.org/show_bug.cgi?id=243324 Attachment 164720: check closeWhenDone pref before opening download window (v2) https://bugzilla.mozilla.org/attachment.cgi?id=164720&action=edit ...

FF4 not automatically opening downloaded PDF files
"Posted earlier but had attachment and doesn't show" I get a Java Script error notice saying that FF can't open a downloaded PDF file because "it probably doesn't exist". I then go to the download indication and right click "open" and Foxit opens the document in a separate window. In FF Tools/Applications I have Foxit set to open PDF documents. In Windows 7 associations, Foxit is also set. This problem only exists on this one laptop computer. Thanks for any help in advance. Richard On 5/3/2011 12:50 PM, Clunker wrote: > "Pos...

PDF File Download Not opening in Adobe Professional
Hello, I have created a pdf download page and works absolutely fine until now :). I have a client and the are using Adobe Professional 6.0. All the PDF documents they open from web open correctly in the application. But since i believe i a using custom download, it does not kick in. Two scenarios: 1. On some windows machines it kicks the Adobe Professional 6.0 application but generates an error 0xc000005 ... 2. On other windows machines it pops up as unknown file type. If they try to open it on a machine which has Adobe Reader, they can open the pdf correctly. So i believe the issue is ...

Web resources about - Download and open PDF file (XE7/Android) [Edit] - embarcadero.delphi.firemonkey

Don't Download This Song - Wikipedia, the free encyclopedia
" Don't Download This Song " is the first single from "Weird Al" Yankovic 's 12th studio album Straight Outta Lynwood . The song was released ...

Facebook Adds To Download Your Information
Facebook increased the amount of information users can access when they download their account histories using the social network’s Download ...

Download the Facebook IQ digital magazine - Facebook for Business
The Facebook IQ digital magazine puts all the insights, best practices and key stats in one place. Download it on your mobile and read it anywhere. ...

Downloads
Download previous versions of the Facebook for iOS SDK.

‘Silent updates’ for Android download latest version of Facebook without Google Play
... users to receive app updates without going through the Google Play store. Some Android users are now receiving “silent updates,” which download ...

Lifehacker, tips and downloads for getting things done
Lifehacker curates tips, tricks, and technology for living better in the digital age.

Technology Product Reviews, News, Prices & Downloads - PCMag.com - PC Magazine
PC Magazine is your complete guide to PC computers, peripherals and upgrades. We test and review computer- and Internet-related products and ...

Indie Music News, MP3 Downloads, Music Videos, Reviews - Stereogum
MP3s downloads, music videos, concert reviews, tour dates and contests. Updated regularly.

RN Download ThisShow (@RNDownload) on Twitter
Log in Sign up You are on Twitter Mobile because you are using an old version of Internet Explorer. Learn more here RN Download ThisShow @ RNDownload ...

Final Fantasy IX is now available for download on iOS w/ special launch pricing
... the game itself, remember, this thing is a beast: “This application takes up around 4GB of memory. Over 8GB of free space is required to download ...

Resources last updated: 2/11/2016 4:02:56 AM