在過去的幾周裏,我一直在使用Android應用程序來獲取圖片以發佈到PHP服務器。我成功地實現了它的功能,並且我能夠在圖像上點擊「分享」,並將其上傳到服務器。應用程序圖片上傳功能停止工作
我一直在向幾個人展示它約3天,突然今天它停止工作。現在,當我點擊「分享」時,它只是說'ERROR website.com',並且文件不會上傳。
我沒有改變任何代碼(我沒有重新上傳Eclipse的任何新代碼,也沒有編輯服務器上的PHP文件),並且好像這個錯誤剛剛彈出無處。由於它只是說「ERROR website.com」,它不是很具描述性,但我可以認爲該網站是問題。所以我檢查了.php文件,它仍然按照預期工作,並且網站沒有崩潰。
有沒有人經歷過類似的任何事情,或者有人知道我能找到答案嗎?
的Android代碼:
public class UploadImage extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_starting_point);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String action = intent.getAction();
// if this is from the share menu
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
try {
// Get resource path from intent callee
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
// Query gallery for camera picture via
// Android ContentResolver interface
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);
// Get binary bytes for encode
byte[] data = getBytesFromFile(is);
// base 64 encode for text transmission (HTTP)
int flags = 1;
byte[] encoded_data = Base64.encode(data, flags);
String image_str = new String(encoded_data); // convert to
// string
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",
image_str));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://xxxx.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this,
"Response " + the_string_response,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "
+ e.toString());
}
}
}
}
PHP代碼:
<?php
if($base=$_REQUEST['image']){
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$destination = time() + rand(1, 1000) . ".jpg";
$url_destination = "project_images/" . $destination;
$file = fopen($url_destination, 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete.';
?>
PHP文件最有可能返回'ERROR website.com'。我建議你發佈你的上傳腳本的代碼 – Ronnie
正如羅尼所說,這還不是足夠的信息。可能有很多原因可能會突然失敗。這聽起來像是你的PHP腳本因爲某種原因拒絕了上傳,但它可能也值得發佈你的Android代碼,所以我們可以看看。 – mattgmg1990
@羅尼 - 我編輯了我的上述帖子以顯示代碼。此外,我編輯了PHP文件並刪除了所有內容,但仍然收到了'ERROR網站'消息,即使這些文字未在PHP文件中列出。 –