Harsha M V
Harsha M V

Reputation: 54949

Uploading Image to PHP Application

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

Upvotes: 0

Views: 579

Answers (1)

Peter Knego
Peter Knego

Reputation: 80330

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(); 
            } 
    } 

Upvotes: 2

Related Questions