1

I am creating a PDF file on android device and trying to write it. However the created file says "not an valid pdf" when I try to open. I get exception in code if I try to write/open.

error:

Unhandled Exception: 10-25 18:51:32.853 E/mono-rt (21231): [ERROR] FATAL UNHANDLED EXCEPTION: System.IO.IOException: Sharing violation on path /sdcard/Download/test5.pdf".

But the file is created in the path. I tried opening manually and got error that the file is not valid.

I am using Xamarin.Android.

Code:

        String filePath;
        FileStream fs;

        filePath = Path.Combine("sdcard/Download/", "test5.pdf");
        fs = new FileStream(filePath, FileMode.Create);


        var pdfBinary = Convert.FromBase64String(buf);

        using (var writer = new BinaryWriter(fs)) {
            writer.Write(pdfBinary, 0, pdfBinary.Length);
            writer.Close();
        }
Vishwas Rao
  • 121
  • 1
  • 1
  • 11
  • most probably your PDF structure is wrong. – user1506104 Oct 25 '16 at 13:16
  • I have not written anything to PDF. I am getting error in the line "var fs = new FileStream(filename, FileMode.Open);". – Vishwas Rao Oct 25 '16 at 13:18
  • What is the value of `filename`? And why are you opening two streams? – greenapps Oct 25 '16 at 13:20
  • `"sdcard/Download/"` That should be `"/sdcard/Download/"` i think. – greenapps Oct 25 '16 at 13:20
  • `I am getting error in the line ... `.You mean the Unhandled Exception? Put that in your post please. You should have told that right away. – greenapps Oct 25 '16 at 13:22
  • Apologies , I have Edited the error message. But manual access still says "Invalid PDF". Exception message received while executing is "ERROR] FATAL UNHANDLED EXCEPTION: System.IO.IOException: Sharing violation on path /sdcard/Download/test5.pdf". – Vishwas Rao Oct 25 '16 at 13:24
  • `But the file is created in the path`. How many bytes? And what is manually? – greenapps Oct 25 '16 at 13:25
  • `Exception message received while executing` ??? What do you mean with while executing? It is very unclear if you talk about your code writing a file or are trying to share a file with an app the user choosed. You mix all up. – greenapps Oct 25 '16 at 13:28

1 Answers1

0

This seems to be related to C# file system sharing violation and not to anything concerning PDF structure: Sharing Violation on Path Error C#

Community
  • 1
  • 1
Vaiden
  • 15,728
  • 7
  • 61
  • 91