1

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);
            fileintent.setType("application/*");
            startActivityForResult(fileintent, RESULT_LOAD_FILE);

        break;

        case R.id.uploadbut:


            break;
    }

}

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

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

                filetoupload.setVisibility(View.VISIBLE);
                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();
                }
            }
    }
}

1 Answers1

0

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

http://hc.apache.org/

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.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
         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() {

                        @Override
                        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());
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194