Dawit Tilahun Davatron Dawit Tilahun Davatron - 12 days ago 10
Android Question

Uploading selected file(PDF, doc) to a server android

I am trying to upload the selected File to a server and i seem to have run into a wall when it comes to starting the upload activity when the "uploadbtn" button is clicked, so the question is which roots should i follow to successfully upload the file selected? any advice is greatly appreciated.
I have completed the php and mysql side of this app, below is the majority of my code except the php and mysql code.

public void onClick(View v) {

switch (v.getId()) {
case R.id.filetoupload:

Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
startActivityForResult(fileintent, RESULT_LOAD_FILE);


case R.id.uploadbut:



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {

if (requestCode == RESULT_LOAD_FILE && resultCode == RESULT_OK
&& null != data) {
Uri selectedPdf = data.getData();

if (selectedPdf.getLastPathSegment().endsWith("pdf")) {

System.out.println("Uri of selected pdf---->" + selectedPdf.toString());
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Invalid file type", Toast.LENGTH_SHORT).show();


I would suggest to use apache library to download your file into server.


So, you need: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.2 https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore/4.4.5 https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime/4.5.2 https://mvnrepository.com/artifact/org.apache.james/apache-mime4j-core/0.7.2

part of code:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();  
//some field      
         builder.addPart("a_simple_text",  new StringBody("sometext",ContentType.TEXT_PLAIN));
//some file
builder.addPart("upload_file", new ByteArrayBody(b,path));

                 HttpEntity entity = builder.build();

                 HttpPost post = new HttpPost(url);

                 OProgressHttpEntityWrapper.ProgressCallback progressCallback = new OProgressHttpEntityWrapper.ProgressCallback() {

                        public void progress(float progress, float totalprogress , long transferred,
                                long totalBytes, long cut_data, long length) {
                            Log.d(TAG, "progress " + progress + " totalprogress " + totalprogress + " transferred " + transferred + " totalBytes " + totalBytes + " cut_data " + cut_data + " length " + length);

                 post.setEntity(new OProgressHttpEntityWrapper(entity, progressCallback, cut_data, length));

 HttpClient client = new DefaultHttpClient();
                 HttpResponse response = client.execute(post);  
                 String html = EntityUtils.toString(response.getEntity());