首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未上传MultipartEntity镜像

未上传MultipartEntity镜像
EN

Stack Overflow用户
提问于 2013-04-12 23:42:35
回答 1查看 1.8K关注 0票数 3

我想用MultipartEntity上传图片。我已经尝试了下面的代码。

我没有得到任何错误,但图像不是上传。我有足够的权限上传

PHP

代码语言:javascript
复制
<?php

    //Receive the data from android
    $name = $_POST['name'];
    $data = $_POST['data'];

    //Receive the file
    $file = $_FILES['image']

    move_uploaded_file($_FILES['image']['tmp_name'], "User_files/".$_FILES['image']['name']);
    //process the data

    //return response to the server

    echo json_encode(
                array(
                    'result'=>'success',
                    'msg'=>'Report added successfully.'
                    )
                );

                ?>

public void upload() throws Exception {
        //Url of the server
        String url = "URL";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        MultipartEntity mpEntity = new MultipartEntity();
        //Path of the file to be uploaded
        String filepath = Environment.getExternalStorageDirectory()+"/a.png";
        File file = new File(filepath);
        ContentBody cbFile = new FileBody(file, "image/*");         

        //Add the data to the multipart entity
        mpEntity.addPart("image", cbFile);
        mpEntity.addPart("name", new StringBody("Test", Charset.forName("UTF-8")));
        mpEntity.addPart("data", new StringBody("This is test report", Charset.forName("UTF-8")));
        post.setEntity(mpEntity);
        //Execute the post request
        HttpResponse response1 = client.execute(post);
        //Get the response from the server
        HttpEntity resEntity = response1.getEntity();
        String Response=EntityUtils.toString(resEntity);
        Log.d("Response:", Response);
        //Generate the array from the response
        JSONArray jsonarray = new JSONArray("["+Response+"]");
        JSONObject jsonobject = jsonarray.getJSONObject(0);
        //Get the result variables from response 
        String result = (jsonobject.getString("result"));
        String msg = (jsonobject.getString("msg"));
        //Close the connection
        client.getConnectionManager().shutdown();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-15 14:38:27

您需要在清单文件中授予WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE的权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15975577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档