0

I have been looking at a tutorial to upload an Image to a Server and I want to alter the code so that I can upload a PDF.

I'm a little confused if the bitmap information would change or not?

This is the code I'm looking at:

 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;
}

private void uploadImage(){
    class UploadImage extends AsyncTask<Bitmap,Void,String>{

        ProgressDialog loading;
        UploadRH rh = new UploadRH();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(UploadActivity.this, "Uploading...", null,true,true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Bitmap... params) {
            Bitmap bitmap = params[0];
            String uploadImage = getStringImage(bitmap);

            HashMap<String,String> data = new HashMap<>();

            data.put(UPLOAD_KEY, uploadImage);
            String result = rh.sendPostRequest(UPLOAD_URL,data);

            return result;
        }
    }

    UploadImage ui = new UploadImage();
    ui.execute(bitmap);
}

What would I need to change in the code?

1 Answers1

0

Base64 is commonly used to send images, I would do something more close to this: Upload pdf file from android to php

Otherwise if you still want to use base64 maybe this is something to check out: Convert a file (<100Mo) in Base64 on Android

OKHttp http://square.github.io/okhttp/ is a library that does the heavylifting. Check out Retrofit too: http://square.github.io/retrofit/ that uses OKHttp

Community
  • 1
  • 1