Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Can't download and open local PDF file on Android tablet device using Xamarin forms PCL

$
0
0

Hi, I am trying to write a test app in debug mode using the dependency service from Xamarin.forms to download a PDF file from the internet onto my app's special folder on my Lenovo Android tablet device, see code below for download process:

string url = "http://website_domain/file.pdf" // An example
var newFilename = "test.pdf"; // My new filename to be stored on local device
string documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
var fullpath = Path.Combine(documentsPath, newFilename );

        if (!Directory.Exists (documentsPath)) {
            Console.WriteLine ("Creating Folder: " + documentsPath);
            Directory.CreateDirectory (documentsPath);
        }

        if (File.Exists (fullpath)) {
            Console.WriteLine ("File already exists: " + fullpath);
            return; // Don't re-download file
        } else {
            // Download PDF to local device
            Uri baseUri = new Uri (url);
            HttpClientHandler clientHandler = new HttpClientHandler ();
            HttpClient httpClient = new HttpClient (clientHandler);
            httpClient.BaseAddress = baseUri;
            byte[] imageBytes = await httpClient.GetByteArrayAsync (baseUri);
            File.WriteAllBytes (fullpath, imageBytes);

            Console.WriteLine ("Downloaded to: " + fullpath + ", File Size: " + imageBytes.Length);
        }

The destination file path (fullpath) of the PDF on the device is:

"/data/data/com.companyname.test1/files/test.pdf"

The code above seem to work fine, the problem is I can't see the test.pdf file on the Android device using the "Root Browser" app or other file browser. When my app then attempts to open the local PDF file, it returns an error message from the device's external PDF reader (Drive PDF Reader) reports: "Can't display PDF (test.pdf cannot be opened)". All the other PDF readers have their own similar error messages.
The external PDF reader code:

        Java.IO.File file = new Java.IO.File(fullpath); 
        file.SetReadable(true);
        //Android.Net.Uri uri = Android.Net.Uri.Parse("file://" + fullpath);
        Android.Net.Uri uri =  Android.Net.Uri.FromFile(file);
        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(uri, "application/pdf");
        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

        try
        {
            Xamarin.Forms.Forms.Context.StartActivity(intent);
        }
        catch (Exception)
        {
            Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View PDF", ToastLength.Short).Show();
        }

To help resolve to this problem, I have added the line below to the Android.Manifest.xml file:


Also tried adding the + @"/" + between folder name and file name with on joy :o(

To summaries my problem:

1) I'm not sure if my local test.pdf file is downloaded on the device or not, although the statement 'File.Exists (localPath)' says it is?
2) I'm not sure if the possible downloaded file got corrupted during the download process?
3) I'm using Xamarin Studio with a Xamarin Forms project. Is my code wrong and/or a Xamarin project setting related to this issue I'm unaware of?
4) Or is my Lenovo Android v4.4.2 somehow not permitting the test app proper read/write access to the device?

Last point similar problems happen on the Android simulator when I perform a download test!

Can someone please help as I am banging my head again a brick wall, I can't seem to find a solution for this problem anywhere.

Much appreciated


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>