Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

file URI which is getting from Adobe application is not able to access in my application
Posted
Updated 31-Aug-18 6:02am
v2
Comments
Richard MacCutchan 31-Aug-18 6:32am    
Maybe the file does not exist at that location, or root_external does not exist on the Android device. Only you can find the answer, as we do not have access to your system(s).

1 solution

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;
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900