0

I want to start the camera and click a photo and store it in the SD card and it should automatically choose the particular file and give an option to upload with the image being displayed on the screen.

On click of upload it should upload to the server. I wanna know how to handle the file uploading in PHP via Android application too.

I am new to android. Please guide me

1 Answer 1

2

Take from this thread:

public void doUpload(String filepath,String filename) { 
            HttpClient httpClient = new DefaultHttpClient(); 
            try { 
                    httpClient.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second 
                    post = new HttpPost(new URI(YOUR_SERVER_ADDRESS)); 
                    File file = new File(filepath); 
                    FileEntity entity; 
                    if (filepath.substring(filepath.length()-3, filepath.length ()).equalsIgnoreCase("txt") || 
                            filepath.substring(filepath.length()-3, filepath.length ()).equalsIgnoreCase("log")) { 
                            entity = new FileEntity(file,"text/plain; charset=\"UTF-8\""); 
                            entity.setChunked(true); 
                    }else { 
                            entity = new FileEntity(file,"binary/octet-stream"); 
                            entity.setChunked(true); 
                    } 
                    post.setEntity(entity); 
                    post.addHeader(FILENAME_STR, filename); 
                    HttpResponse response = httpClient.execute(post); 
                    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { 
                            Log.e(TAG,"--------Error--------Response Status line code:"+response.getStatusLine()); 
                    }else { 
                            // Here every thing is fine. 
                    } 
                    HttpEntity resEntity = response.getEntity(); 
                    if (resEntity == null) { 
                            Log.e(TAG,"---------Error No Response !!!-----"); 
                    } 
            } catch (Exception ex) { 
                    Log.e(TAG,"---------Error-----"+ex.getMessage()); 
                    ex.printStackTrace(); 
            } finally { 
                      httpClient.getConnectionManager().shutdown(); 
            } 
    } 
Sign up to request clarification or add additional context in comments.

5 Comments

so basically it acts as a form which posts the file to that page ?
I was assuming that you have a file upload handler (mime-multipart) on your PHP server. Right?
this is the first time i am trying to communicate android to php. i know how to write php file handler.. is it the same way ? $_POST should give me all the content then right ?
can u give me an example how to use this method.. i tried but its not working for me..
I don't know PHP so I can tell you what might be wrong. Do you get some error?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.