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;
}
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. – blackappsgetStringImage(bitmap,.....
What is that function doing? Post the code too please. In your post. – blackapps