2012-04-26 8 views
0

私は自分のカメラで撮った写真をアップロードするプログラムを探していて、自分のウェブサイトにビットマップで保存しています。私は次のコードを試みましたが、うまくいきませんでした。自分のウェブサイトに写真をアップロード

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

私はローカルホスト上のPHPファイルをしようとすると、それはtest.jpgを作るです。だから私は$_REQUEST['image']がnullでない場合、正しいファイルが作成されると思います。

tryエリアにエラーがない場合、androidprogramはtoasttext 'OK'を表示します。エラーがあれば 'FOUT'と表示されます。私のXperiaでテストするとき、それは常にOKを示します...

何か提案がありますか?

答えて

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()); 
    } 
} 
を試してみてください
関連する問題