【问题标题】:Uploading photo to own website上传照片到自己的网站
【发布时间】:2012-10-05 21:02:10
【问题描述】:

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

        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代码是

<?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 />';
?>

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

如果尝试区域中没有错误,我的 androidprogram 会显示一个 toasttext 'OK'。如果有错误,它会显示“FOUT”。在我的 Xperia 上测试时,它总是显示 OK...

有什么建议吗?

【问题讨论】:

    标签: android image-uploading


    【解决方案1】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 2010-10-25
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多