2012-09-25 201 views
0

我跟着教程如何上傳圖像,但文件不出現,我不知道我在哪裏做錯了。基本上,我從手機庫中選擇一張照片併發送。上傳圖片ANDROID

下面是我的代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home); 

    btnUpload = (Button) this.findViewById(R.id.btnUpload); 

    btnUpload.setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      // To open up a gallery browser 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(
        Intent.createChooser(intent, "Select Picture"), 1); 
     } 
    }); 

    btnSent = (Button) this.findViewById(R.id.btnSent); 

    btnSent.setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      HttpUploader uploader = new HttpUploader(); 
      try { 
       // String image_name = 
       uploader.execute(getRealPathFromURI(currImageURI)).get(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

// To handle when an image is selected from the browser 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      // currImageURI is the global variable I’m using to hold the 
      // content: 
      currImageURI = data.getData(); 
      path = (TextView) findViewById(R.id.path); 
      path.setText(getRealPathFromURI(currImageURI)); 
     } 
    } 
} 

// Convert the image URI to the direct file system path of the image file 
public String getRealPathFromURI(Uri contentUri) { 
String[] proj = { MediaStore.Images.Media.DATA }; 
    android.database.Cursor cursor = managedQuery(contentUri, proj, // Which 
                    // columns 
                    // to 
                    // return 
      null, // WHERE clause; which rows to return (all rows) 
      null, // WHERE clause selection arguments (none) 
      null); // Order-by clause (ascending by name) 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

這裏是我的http:

for (String sdPath : path) { 
     Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath); 
     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); 

     // Here you can define .PNG as well 
     bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
     byte[] ba = bao.toByteArray(); 
     String ba1 = Base64.encodeBytes(ba); 

     System.out.println("uploading image now ——–" + ba1); 

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("image", ba1)); 

     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(
        "http://path to my server/upload.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 

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

      // is = entity.getContent(); 
      Log.e("log_tag ******", "good connection"); 

      bitmapOrg.recycle(); 

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

這是我的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

您能查看的error_log你的PHP服務器上的樣子?你在PHP方面遇到什麼錯誤? – GBD

+0

沒有錯誤。它說好連接 –

回答

0

釷腳本是否正在工作,您可以在servar上的上傳文件夾中找到該圖像。

<?php 
$target = "upload/"; 

$target = $target . basename($_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
    echo "yes"; 
} 
else { 
    echo "no"; 
} 
?> 

對這個answer

+0

哪裏可以找到服務器上的上傳文件? –

+0

你可以在上傳文件夾中找到該文件 –

+0

最新的路徑?我找不到。對不起,我還是新的 –