1
votes

I am trying to upload PDF file to my server. I am able to upload images decoding using bitmap. I am reusing the same code but I need to upload PDF from a user on my Android app.

private void chooseFile(int type2) {
    //System.out.println(type2);
    Intent intent = new Intent();
    //intent.setType("application/pdf");
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),type2 );
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //  System.out.println(data);
    if ((requestCode == 1 || requestCode == 2 || requestCode == 3 || requestCode == 4 || requestCode == 5 || requestCode == 6 || requestCode == 7 || requestCode == 8) && resultCode == RESULT_OK && data != null && data.getData() != null) {
         Uri filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            // profile_image.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (requestCode == 1) {
            String userString = getStringImage(bitmap , 1);
            RequestQueue requestQueue = Volley.newRequestQueue(CashFlowActivity.this);
            String url = C.url+"cashflow_upload.php";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //System.out.println(response);
                        // Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                    p1 = "user_file/u_image/"+userID+"/extract.pdf";
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Error while uploading image", Toast.LENGTH_SHORT).show();
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("userId", userID);
                    params.put("extractImage",userString);
                return params;
                }
            };

            requestQueue.add(stringRequest);

I am not getting a direct method to upload PDF through Android app associated with PHP, MySQL. I did some changes here and there I am able to get a PDF file on my server but the PDF itself is empty or contains the string of location of the file.

public String getStringImage(Bitmap bitmap, int type) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

    byte[] imageByteArray = byteArrayOutputStream.toByteArray();
    String encodeImage = Base64.encodeToString(imageByteArray, Base64.DEFAULT);

    imageByteArray = Base64.decode(encodeImage, Base64.DEFAULT);
    Bitmap decodeImage = BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
    if (type == 1) {
        extractPhoto.setImageBitmap(decodeImage);
    } else if (type == 2) {
        bankPhoto.setImageBitmap(decodeImage);
    } else if (type == 3) {
        itrPhoto.setImageBitmap(decodeImage);
    } else if (type == 4) {
        bankPhoto1.setImageBitmap(decodeImage);
    } else if (type == 5) {
        itrPhoto1.setImageBitmap(decodeImage);
    } else if (type == 6) {
        bankPhoto2.setImageBitmap(decodeImage);
    } else if (type == 7) {
        itrPhoto2.setImageBitmap(decodeImage);
    } else if (type == 8) {
        bankPhoto3.setImageBitmap(decodeImage);
    }
    return encodeImage;
}
1
I am able to upload images decoding using bitmap. That is the wrong approach as why would you first convert a file to a bitmap? Upload the bytes of the file directly. Then it does not matter what kind of file you upload. You are ready for all files then.blackapps
getStringImage(bitmap,..... What is that function doing? Post the code too please. In your post.blackapps
@blackapps But how should I upload directly what function should I write? see the getStringImage function in the editNeel Patel
Your function compresses a bitmap to a jpg. And the jpg is finally in byte[] imageByteArray. After that you base64 encode byte[] imageByteArray and you have a string (which contains a base64 encoded jpg file) which you upload. Now how did you get that bitmap? You made that bitmap from a choosen image file.blackapps
Do away with the bitmap. Load the bytes of the choosen file directly in byte[] imageByteArray. Then encode and so on.blackapps

1 Answers

1
votes

You can use this Function:

public String getStringPdf (Uri filepath){
    InputStream inputStream = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
       inputStream =  getContentResolver().openInputStream(filepath);

        byte[] buffer = new byte[1024];
        byteArrayOutputStream = new ByteArrayOutputStream();

        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    byte[] pdfByteArray = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(pdfByteArray, Base64.DEFAULT);
}

Thanks to blackapps for guiding correct method.