Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Unable to access the adobe application shared pdf file in android application

KorlakuntaBabuKorlakuntaBabu USMember ✭✭
edited August 2018 in Xamarin.Android

I am working android application development process and I am sharing pdf file from Adobe app to my android application. I am getting shared pdf file URI as follows

    Android.Net.Uri fileuri =
    (Android.Net.Uri)Intent.GetParcelableExtra(Intent.ExtraStream);

    fileuri i am getting as {content://com.adobe.reader.fileprovider/root_external/
                                        data/data/com.adobe.reader/files/Downloads/sample.pdf}

    string filePath = fileuri.Path;

   filePath I am gettings as root_external/data/data/com.adobe.reader/files/Download/sample.pdf

    File.Exist(filePath)--> returns false

I don't what exactly the reason behind not accessing the pdf file
please help me to access the Adobe shared a file in my android application

Tagged:

Answers

  • KorlakuntaBabuKorlakuntaBabu USMember ✭✭
    edited August 2018

    By the following code, I am able to get adobe application shared pdf file as a stream and saving into android application path

    using (var stream = ContentResolver.OpenInputStream(fileuri))
    {
           byte[] fileByteArray = ToByteArray(stream); //only once you can read bytes from stream second time onwards it has zero bytes
    
           string fileDestinationPath ="<path of your destination> "
           convertByteArrayToPDF(fileByteArray, fileDestinationPath);//here pdf copied to your destination path
    }
         public static byte[] ToByteArray(Stream stream)
            {
                var bytes = new List<byte>();
    
                int b;
                while ((b = stream.ReadByte()) != -1)
                    bytes.Add((byte)b);
    
                return bytes.ToArray();
            }
    
          public static string convertByteArrayToPDF(byte[] pdfByteArray, string filePath)
            {
    
                try
                {
                    Java.IO.File data = new Java.IO.File(filePath);
                    Java.IO.OutputStream outPut = new Java.IO.FileOutputStream(data);
                    outPut.Write(pdfByteArray);
                    return data.AbsolutePath;
    
                }
                catch (System.Exception ex)
                {
                    return string.Empty;
                }
            }
    
Sign In or Register to comment.