2012-03-03 52 views
3

私のサーバーに画像をアップロードしようとすると、今は失われています。私は写真を撮って、Androidデバイス上で自分の位置を取得することができます。ファイルをサーバーにアップロードするためのコードがあります。サーバーに画像をアップロードするPHP Android

public Boolean postFunction(File image) { 
    String tag = "postFunction"; 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(UPLOAD_URL); 

    try { 
     MultipartEntity entity = new MultipartEntity(); 

     entity.addPart("type", new StringBody("photo")); 
     entity.addPart("data", new FileBody(image)); 
     httppost.setEntity(entity); 
     HttpResponse response = httpclient.execute(httppost); 
     Log.i(tag, "picture was uploaded " + response.toString()); 
     return true; 

    } catch (ClientProtocolException e) { 
     Log.e(tag, "Client: " + e.toString()); 
     return false; 
    } catch (IOException e) { 
     Log.e(tag, "IO: " + e.toString()); 
     return false; 
    } 
} 

私はこの機能に画像ファイルを渡してアップロードします。しかし、エラーは私が信じるサーバー側にある。ここで

は私のPHPコードです:

 public function upload() { 
     //$type = $this->input->post('type'); 
     //$data = $this->input->post('data'); 

     $base = $_REQUEST['data']; 

     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>'; 
    } 

ファイルが触れたが、まだ空白のままされます。私はここに何かを逃していますか助けてください、私はこれをグーグルで試しましたが、私にはあまり役に立たなかった別の結果が出ました。

答えて

3

非常に良いチュートリアルhereを確認できます。

また(

public class TryprojectActivity extends Activity { 
    InputStream is; 
    int pic_count = 0; 
    Bitmap bitmap=null; 
    FileInputStream in1,in2,in3; 
    BufferedInputStream buf; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

     try { 
      in1 = new FileInputStream("/sdcard/1.jpg"); 
     } 
     catch (FileNotFoundException e2) { 
     // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 

     try { 
      in2 = new FileInputStream("/sdcard/2.jpg"); 
     } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    try { 
     in3 = new FileInputStream("/sdcard/3.jpg"); 
    } 
    catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    Bitmap bitmapOrg1 = BitmapFactory.decodeStream(in1); 
    ByteArrayOutputStream bao1 = new ByteArrayOutputStream(); 
    bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1); 
    byte [] imagearray1 = bao1.toByteArray(); 
    String ba1=Base64.encode(imagearray1); 

    Bitmap bitmapOrg2 = BitmapFactory.decodeStream(in2); 
    ByteArrayOutputStream bao2 = new ByteArrayOutputStream(); 
    bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2); 
    byte [] imagearray2 = bao2.toByteArray(); 
    String ba2=Base64.encode(imagearray2); 

    Bitmap bitmapOrg3 = BitmapFactory.decodeStream(in3); 
    ByteArrayOutputStream bao3 = new ByteArrayOutputStream(); 
    bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3); 
    byte [] imagearray3 = bao3.toByteArray(); 
    String ba3=Base64.encode(imagearray3); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 

    nameValuePairs.add(new BasicNameValuePair("image1",ba1)); 
    nameValuePairs.add(new BasicNameValuePair("image2",ba2)); 
    nameValuePairs.add(new BasicNameValuePair("image3",ba3)); 

    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://helpdesk.cispl.com/upload_file.php"); 
     UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs); 
     obj.setChunked(true); 
     httppost.setEntity(obj); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     //is = entity.getContent(); 
     httpclient.getConnectionManager().shutdown(); 
    } 
    catch(Exception e){ 
     //CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString()); 
     //CommonFunctions.showToast(ctx, "Unable to post captured image file: " + 
     //e.toString()); 
    } 
} 
0

UNIXベースのプラットフォーム上でApache/PHPを実行している場合、Apacheサーバは、中にファイルを保存しようとしているディレクトリへの書き込み権限を持っていることを確認し、次のコードしてみてくださいあなたの現在のコードに従って、PHPスクリプトが置かれているのと同じディレクトリ)。クイックテストとして、あなたのスクリプトが入っているディレクトリにchmod 0777 .を入れて、アップロードされた画像が正しく表示されるか確認してください。

関連する問題