开发者

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开发者_StackOverflow社区 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜