0

カメラで撮った写真をビットマップ形式で自分の Web サイトにアップロードするプログラムを探していました。次のコードを試しましたが、失敗しました。

        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']したがって、null でない場合は、正しいファイルが作成されると思います。

トライエリアにエラーがない場合、私のアンドロイドプログラムはトーストテキスト「OK」を表示します。エラーがある場合は「FOUT」と表示されます。私のXperiaでテストすると、常にOKと表示されます...

助言がありますか?

4

1 に答える 1

0

これを試して

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-04-27T04:46:38.927 に答える