将图像上传到 PHP 应用程序

发布于 2024-11-05 06:45:05 字数 140 浏览 0 评论 0原文

我想启动相机并单击照片并将其存储在 SD 卡中,它应该自动选择特定文件并提供上传选项,图像显示在屏幕上。

单击上传后,它应该上传到服务器。我也想知道如何通过 Android 应用程序处理 PHP 中的文件上传。

我是安卓新手。请指导我

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过气美图社 2024-11-12 06:45:05

摘自此帖子

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

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(); 
            } 
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文