将Android应用程序中捕获的文件上载到桌面上的远程服务器

时间:2012-04-30 08:21:06

标签: php android

我刚刚完成了我的Android应用程序,它使用本机手机摄像头拍照并录制视频。我现在想用我的手机在我的本地网站远程上传这些项目(Apache作为网络服务器,MySQL作为数据库脚本语言是php)。但是,该网站在本地运行。我希望创建一个临时的。有人可以帮助我使用正确的参数在手机上设置我的提交按钮,以便在我的本地Web应用程序上成功上传我的数据。 Thanx提前。

2 个答案:

答案 0 :(得分:1)

要在Android上传图像或视频,通常会使用Multipart请求,请查看。 This link给出了android中multipart的基本介绍。

答案 1 :(得分:1)

正如@Antrromet所说,你必须使用带有Multipart的HttpPost发送你的文件。

这是使用httpmime apache库(查看MultipartEntity)在多部分中发送Bitmap的方法示例:

public String doHttpMultipart(String url, 
                              List<NameValuePair> pairs,
                              Bitmap bitmap,
                              String fileName) throws IOException, 
                                            ClientProtocolException,
                                            UnsupportedEncodingException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();        
        bitmap.compress(CompressFormat.PNG, 100, bos);
        byte[] imageData = bos.toByteArray();
        ByteArrayBody byteArrayBody = new ByteArrayBody(imageData, fileName);
        MultipartEntity reqEntity = 
                    new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        reqEntity.addPart("image", byteArrayBody);

        for(NameValuePair p : pairs) {
            reqEntity.addPart(p.getName(), new StringBody(p.getValue()));
        }

        HttpPost request = new HttpPost(url);
        request.setEntity(reqEntity);

        HttpClient client = new DefaultHttpClient();
        HttpResponse httpResponse = client.execute(request);

        String response = "";
        BufferedReader in = null;

        try {
            response = super.readHttpStream(response, in, httpResponse);    
        } catch(IllegalStateException e) {
            throw new IllegalStateException();
        } catch(IOException e) {
            throw new IOException();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return response;
    }

在这种方法中,我还会根据我的请求发送参数列表,并从服务器读取响应。

在后端,您将收到$_FILES变量中的二进制数据。