2

I am trying to convert bytes to pdf in xamarin android application using c#. In which bytes are coming from webservice. I am using simple webservice(asmx) for bringing data.

 appLoginService = new EgrasAndroid.AppLoginService();
 byte[] grnbytedata= appLoginService.GetGRNPdf(UserId.ToString(), GRN.ToString());
 string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
 string file = Path.Combine(directory, "temp.pdf");
 System.IO.File.WriteAllBytes(file, grnbytedata);

I have searched for many solution but they mostly work for web application or java application.

Its not showing any error. I am running this on emulator but there is file shown in downloads folder. while it is taking the path "/storage/sdcard/Download" to download the file.

There is 200mb blank space, i have taken for sdcard in emulator.

4
  • 1
    What's wrong with WriteAllBytes? Any errors? Did you try to debug it? Does grnbytedata contains any value? It is a little bit unclear what you're asking. Please add more details Commented Oct 20, 2016 at 6:09
  • grnbytedata returning data well.. there is no error by WriteAllbytes but no files are shown after downloading.. I am not able to see the file anywhere on emulator.. Commented Oct 20, 2016 at 7:46
  • You might not have permision to write data - check they are on! Commented Oct 20, 2016 at 8:27
  • @DanyDaKur Yeah, I have already checked Read and WRITE_EXTERNAL_STORAGE in android manifest Commented Oct 20, 2016 at 9:21

2 Answers 2

2

The problem was: Emulator does not have pdf reader in itself. so it can't read pdf file. In emulator you have to download PDF reader. Else It is working fine in real device.

If there is no external storage in device or emulator then we can go for internal storage using:

  var directory = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
  directory= Path.Combine(directory ,Android.OS.Environment.DirectoryDownloads);
  string file = Path.Combine(directory.ToString(), "temp.pdf");
  System.IO.File.WriteAllBytes(file, grnbytedata);
Sign up to request clarification or add additional context in comments.

Comments

0

Check your path. There is changes to how access files from Marshmallow. Code below should do the trick

      if (Int32.Parse(Android.OS.Build.VERSION.Release.Substring(0, 1)) > 5)
            {
                documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            }
            else
            { 
                documentsPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads); 
            }

1 Comment

Thanks but you are saying about java code. there is nothing like Environment.GetFolderPath in c#. Path is okay but problem was there is no pdf reader in emulator.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.