首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传照片到自己的网站

上传照片到自己的网站
EN

Stack Overflow用户
提问于 2012-04-26 21:10:25
回答 1查看 301关注 0票数 0

我一直在寻找一个程序来上传我用相机拍摄的照片,并存储在位图中到我自己的网站上。我尝试过跟随代码,但没有成功。

代码语言:javascript
复制
        public void onClick(View v) {
            ByteArrayOutputStream bytearrayStream = new ByteArrayOutputStream();
            bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 90, bytearrayStream);
            byte [] byteArray = bytearrayStream.toByteArray();
            String byteArrayToString = Base64.encodeBytes(byteArray);
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("image", byteArrayToString));

            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.mywebsite.com/upload_image.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                stream = entity.getContent();

                Toast text = Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG);
                text.show();
            }
            catch(Exception e){
                Log.e("log_tag", "Error in http connection " + e.toString());
                Toast text = Toast.makeText(getApplicationContext(), "FOUT", Toast.LENGTH_LONG);
                text.show();

            }
        }

我在网站端使用的php代码是

代码语言:javascript
复制
<?php
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
 $binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg />';
?>

如果我在本地主机上尝试php文件,它会生成一个test.jpg。所以我认为如果$_REQUEST['image']不是null,就会创建正确的文件。

如果try-area中没有错误,我的area程序会显示一个toasttext 'OK‘。如果有错误,它会显示'FOUT‘。在我的Xperia上测试时,总是显示OK...

有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2012-04-27 12:46:39

尝尝这个

代码语言:javascript
复制
public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
 bm.compress(CompressFormat.JPEG, 75, bos);
 byte[] data = bos.toByteArray();
 HttpClient httpClient = new DefaultHttpClient();
  HttpPost postRequest = new HttpPost("http://www.mywebsite.com/upload_image.phpamp");
 ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");

  MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
 reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
 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);
  }
 System.out.println("Response: " + s);
 } catch (Exception e) {
   // handle exception here
   Log.e(e.getClass().getName(), e.getMessage());
  }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10334109

复制
相关文章

相似问题

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