今天就跟大家聊聊有關android中怎么將圖片上傳到PHP,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
復制代碼 代碼如下:
[java] view plaincopyprint?
//設置超時時間
httpclient.setTimeout(20000);
再building,runing,還是不行。。。。這就怪了,明明好好的,怎么會突然就變成連接超時了呢!又折騰了一陣子后,也跟后臺那邊的朋友溝通過,他也測試了上傳接口,發現沒什么問題,就讓我自己去折騰去了。。。。
我就郁悶了,看不出原先的代碼有什么錯誤,也沒什么法子了,就出最下下策吧,自己搭一個PHP上傳圖片接口,親自測試下到底是怎么回事。。。。
1.首先,你得下一個方便快捷的PHP服務器,我這里用了WampServer,百度----下載-----安裝-----啟動,瀏覽器輸入:http://127.0.0.1 有頁面顯示,OK了。就這么簡單!
2.瀏覽器輸入 : http://本機IP地址 回車, 發現報錯,類似“You don't have permission to access / on this server” 說明你的WM還沒設置,需要進行如下設置:
造成這個問題的原因是Apache 的http.conf內的默認配置是
復制代碼 代碼如下:
# onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
只允許127.0.0.1訪問,點擊wampserver圖標讓后點擊Putonline,http.conf內的以上默認配置自動修改為
復制代碼 代碼如下:
# onlineoffline tag - don't remove
Order Allow,Deny
Allow from all
現在localhost可以訪問了。
同樣phpMyadmin在localhost下不能正常訪問在127.0.0.1能正常訪問,解決方法:
點擊根目錄下的alias目錄,打開phpmyadmin.conf配置文件,和上面修改http.conf一樣把
復制代碼 代碼如下:
Deny from all
Allow from 127.0.0.1
修改為
Allow from all
3. 再此輸入 : http://本機IP地址 回車 顯示頁面 OK! 至于為什么要第二步、第三步呢,我就不說了。。。留給新人去想想吧! 大神直接無視。。。。。
4.寫一個上傳圖片的PHP文件,當然我一個敲java的孩子一下子怎么可能憋的出來,那怎么辦,當然是百度參考別人的了,下面的PHP代碼源自網絡,親測沒有錯誤:
[php] view plaincopyprint?
<?php
$base_path = "./upload/"; //存放目錄
if(!is_dir($base_path)){
mkdir($base_path,0777,true);
}
$target_path = $base_path . basename ( $_FILES ['attach'] ['name'] );
if (move_uploaded_file ( $_FILES ['attach'] ['tmp_name'], $target_path )) {
$array = array (
"status" => true,
"msg" => $_FILES ['attach'] ['name']
);
echo json_encode ( $array );
} else {
$array = array (
"status" => false,
"msg" => "There was an error uploading the file, please try again!" . $_FILES ['attach'] ['error']
);
echo json_encode ( $array );
}
?>5.將上面的php文件放在WM安裝目錄下的www目錄下,我的如下圖所示,僅供參考:

6.經過上面幾個步驟,PHP端已經搭建好了,現在就是回到android端改改IP地址測試下就oK了,代碼段如下:
[java] view plaincopyprint?
//HTTP上傳圖片
RequestParams params = new RequestParams();
try {
//將壓縮后的bitmap保存為圖片文件
String saveImgPath=getSD_Path()+"/saveimg.png";
File saveimg=new File(saveImgPath);
FileOutputStream fos = new FileOutputStream(saveimg);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
//上傳壓縮后的文件,大約100k左右
File uploadImg=new File(saveImgPath);
<span >params.put("attach", uploadImg);</span>
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//上傳地址
String url=URLConfigs.UploadHeadImage_ukey+myprefs.Ukey().get();
<span >String url="http://192.168.0.8/upload.php";</span>
LogUtil.e(TAG, "upload img url :"+url);
AsyncHttpUtil.post_loading(context,url, params, new MyTextHttpResponseHandler() {
@Override
public void onSuccess(int status, Header[] arg1, String json) {
super.onSuccess(status, arg1, json);
LogUtil.e(TAG, "上傳圖片 json :"+json);
RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
if(entity.isStatus()){
//上傳成功,設置圖片
face.setImageBitmap(bmp);
ToastUtils.show(context, "上傳成功");
}else{
ToastUtils.show(context, json);
}
myprefs.position().put(0);
}
@Override
public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
super.onFailure(arg0, arg1, arg2, arg3);
myprefs.position().put(0);
arg3.printStackTrace();
ToastUtils.show(context, R.string.network_unavailable);
}params.put("attach", uploadImg); 這里的attach參數是和服務端一一對應的,別亂改。。。。
String url="http://192.168.0.8/upload.php"; 這個192.168.0.8是我的PHP部署的地址,改成你自己的就行了。
PS:別犯2,用了127.0.0.1 想想為啥不能用127.0.0.1
到此就是building,runing了。 發現OK。。。。 可以上傳,并在www目錄下找到upload目錄,upload目錄下有上傳的圖片。。。。

看完上述內容,你們對android中怎么將圖片上傳到PHP有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。