2013-05-06 148 views
1

我使用相机意图捕获图像,然后将图像上传到php web服务器。没有错误产生,但当我看着目的地图像的文件夹,没有任何。 下面的代码:从android上传图像到php web服务器不工作

//for camera intent 

    addphoto.setOnClickListener(new OnClickListener() { 
    //LAUNCH CAMERA 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent cameraintnent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraintnent, 1); 



    } 
}); 

,然后这是onActivityResult()方法的代码

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
if(requestCode==1) { 
    //for camera   

    bitmapOrg= (Bitmap) data.getExtras().get("data");    
    String outPut = null; 

    ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

    //Resize the image 
    double width = bitmapOrg.getWidth(); 
    double height = bitmapOrg.getHeight(); 
    double ratio = 400/width; 
    int newheight = (int)(ratio*height);   
    bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true); 

    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao); 
    byte[] ba = bao.toByteArray(); 
    String ba1 = Base64.encodeBytes(ba); 

    ArrayList<NameValuePair> nameValuePairs = new 

      ArrayList<NameValuePair>(); 

      nameValuePairs.add(new BasicNameValuePair("image",ba1)); 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://localhost/m/upload.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      Log.d("image uploading","The image is uploading"); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity();    

     // print response 
     outPut = EntityUtils.toString(entity); 
     Log.i("GET RESPONSE—-", outPut); 

     //is is the InputStream declared globally within the class 
     is = entity.getContent(); 
     Log.e("log_tag ******", "good connection"); 

     bitmapOrg.recycle(); 

    } catch (Exception e) { 
     Log.e("log_tag ******", "Error in http connection " + e.toString()); 
    } 


    image.setImageBitmap(bitmapOrg); 
    setContentView(parentContainer); 

} 

这是PHP代码:

<?php 

$base = $_REQUEST["image"]; 

if (isset($base)) { 

$suffix = createRandomID(); 
$image_name = "img_".$suffix."_".date("Y-m-d-H-m-s").".jpg"; 

// base64 encoded utf-8 string 
$binary = base64_decode($base); 

// binary, utf-8 bytes 

header("Content-Type: bitmap; charset=utf-8"); 

$file = fopen("../images/post_images/" . $image_name, "wb"); 

fwrite($file, $binary); 

fclose($file); 

die($image_name); 

} else { 

die("No POST"); 
} 

function createRandomID() { 

$chars = "abcdefghijkmnopqrstuvwxyz?"; 
//srand((double) microtime() * 1000000); 

$i = 0; 

$pass = ""; 

while ($i <= 5) { 

$num = rand() % 33; 

$tmp = substr($chars, $num, 1); 

$pass = $pass . $tmp; 

$i++; 
} 
return $pass; 
} 
?> 

哪来的问题,以及如何解决它?

+0

chec k互联网许可? – 2013-05-06 12:30:07

+0

尝试使用MultipartEntity而不是HttpEntity – Vamshi 2013-05-06 12:40:05

回答

0

Demo

我有文件上传创建doFileUpload()。首先上传简单的文件,然后使用startActivityForResult

private void doFileUpload(){ 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 


     String exsistingFileName = "/sdcard/six.3gp"; 
     // Is this the place are you doing something wrong. 

     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 


     int bytesRead, bytesAvailable, bufferSize; 

     byte[] buffer; 

     int maxBufferSize = 1*1024*1024; 

     String urlString = "http://192.168.1.5/upload.php"; 



     try 
     { 


     Log.e("MediaPlayer","Inside second Method"); 

     FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName)); 



     URL url = new URL(urlString); 

     conn = (HttpURLConnection) url.openConnection(); 

     conn.setDoInput(true); 

     // Allow Outputs 
     conn.setDoOutput(true); 

     // Don't use a cached copy. 
     conn.setUseCaches(false); 

     // Use a post method. 
     conn.setRequestMethod("POST"); 

     conn.setRequestProperty("Connection", "Keep-Alive"); 

     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 


     dos = new DataOutputStream(conn.getOutputStream()); 

     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd); 
     dos.writeBytes(lineEnd); 

     Log.e("MediaPlayer","Headers are written"); 



     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 



     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

     while (bytesRead > 0) 
     { 
     dos.write(buffer, 0, bufferSize); 
     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     } 



     dos.writeBytes(lineEnd); 

     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

     BufferedReader in = new BufferedReader(
         new InputStreamReader(
         conn.getInputStream())); 
      String inputLine; 

      while ((inputLine = in.readLine()) != null) 
       tv.append(inputLine); 




     // close streams 
     Log.e("MediaPlayer","File is written"); 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 


     } 
     catch (MalformedURLException ex) 
     { 
      Log.e("MediaPlayer", "error: " + ex.getMessage(), ex); 
     } 

     catch (IOException ioe) 
     { 
      Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe); 
     } 


     //------------------ read the SERVER RESPONSE 


     try { 
      inStream = new DataInputStream (conn.getInputStream()); 
      String str; 

      while ((str = inStream.readLine()) != null) 
      { 
       Log.e("MediaPlayer","Server Response"+str); 
      } 
      /*while((str = inStream.readLine()) !=null){ 

      }*/ 
      inStream.close(); 

     } 
     catch (IOException ioex){ 
      Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); 
     } 



    } 

}

<uses-permission android:name="android.permission.INTERNET" /> 

Demo

+0

我已经添加了互联网权限,显然仍然没有改变。 感谢您的演示链接,但这是一种不同的情况,因为图像被相机捕获,然后直接上传到服务器,而不保存到SD卡 – user2354633 2013-05-06 12:49:48

+0

首先尝试简单的图像上传,比使用这个feture – 2013-05-06 12:52:34

0

如果你没有得到任何error..then Internet权限是没有问题的.. 我觉得你的网址可能是这个问题..如果你配置你的本地主机,即使你不能从移动设备访问该本地主机的URL ..

相关问题