首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用MultipartEntity上传图像

不使用MultipartEntity上传图像
EN

Stack Overflow用户
提问于 2013-04-15 14:03:09
回答 1查看 817关注 0票数 1

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

但是图像没有被上传。我没有得到任何错误。我已经为此添加了足够的库

代码语言:javascript
复制
  try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(
                    "http://192.168.1.6/uploadimg.php");
            httpClient.getParams().setParameter(
                    CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            File f = null;
            FileBody fo = null;

            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            // code for send image using post method
            f = new File("/mnt/sdcard/a.png");
            fo = new FileBody(f);
            reqEntity.addPart("uploaded", fo);

            Log.i("uploaded", "image added Parameter added");

            postRequest.setEntity(reqEntity);

            HttpResponse response = httpClient.execute(postRequest);

            BufferedReader reader = new BufferedReader(new InputStreamReader(

            response.getEntity().getContent(), "UTF-8"));

            String sResponse;

            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {

                s = s.append(sResponse);

            }
            Log.v("Upload photo", "Response" + s);

            // return getUploadResponce(s.toString());
            // Log.i("Response ", );

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

PHP文件

代码语言:javascript
复制
<?php

$file = $_FILES['uploaded']

move_uploaded_file($_FILES['uploaded']['tmp_name'], $_FILES['uploaded']['name']);

            ?>
EN

回答 1

Stack Overflow用户

回答已采纳

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

似乎在获取图像的路径时出现了问题,改变了单行代码;

使用:

代码语言:javascript
复制
String xtStorageDirectory = Environment.getExternalStorageDirectory()+ "/a.png";
 f = new File(xtStorageDirectory);

而是:

代码语言:javascript
复制
f = new File("/mnt/sdcard/a.png");

并确保您已授予访问清单文件中的external storage的权限。

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

https://stackoverflow.com/questions/16008574

复制
相关文章

相似问题

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