无法在Android应用程序中访问Adobe应用程序共享的pdf文件

时间:2018-08-31 07:08:25

标签: c# android xamarin

我正在使用android应用程序开发过程,并且正在将Adobe应用程序中的pdf文件共享到我的android应用程序中。我正在按以下方式获取共享的pdf文件URI

  

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

     

fileuri我正在作为{content://com.adobe.reader.fileprovider/root_external/data/data/com.adobe.reader/files/Downloads/sample.pdf}

     

string filePath = fileuri.Path;

     

filePath我将成为root_external / data / data / com.adobe.reader / files / Download / sample.pdf

     

File.Exist(filePath)->返回false

我不知道不访问pdf文件的确切原因是什么
请帮助我在我的android应用程序中访问Adobe共享文件

1 个答案:

答案 0 :(得分:0)

通过以下代码,我可以将Adobe应用程序共享的pdf文件作为流获取并保存到android应用程序路径中

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;
            }
        }
相关问题