2

I am currently making an app for students where they can upload a PDF file to a server. I am using the android Volley API but have been testing the function using JPEG files.

This is my code

  public String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

How would I change this code so that a PDF can be uploaded instead?

Do I still used Base64? and imageBytes?

Or is there an alternate method?

1 Answers1

0

You should be able to reuse most of the code that you have there, instead of taking in a Bitmap, you would take in a File. You won't be able to use Bitmap.Compress on it though so that will need to be removed.

For uploading a PDF, you can Base64 encode it and send it as a string as part of the request, this might get out of control if you are handling large files.

The other option is to use a multipart form, I would suggest taking a look at this stackoverflow question and answer for how to do that.

Community
  • 1
  • 1
Brandon Haugen
  • 961
  • 6
  • 25