» 首頁 » 討論區 » Android程式設計 »android php mysql 上傳圖片問題

android php mysql 上傳圖片問題

發表人: ooaaa
積分: 158
發表時間: 2011-07-25 03:51:44
各位大大...小弟最近卡在 上傳圖片上面....先講一下小弟程式流桯

android <->php <-> mysql

不知道我的android 程式應該怎樣打才可以上傳圖片到我的apache....可否給個簡單的範例...拜託...我在網路上找到n個版本也是不行...有大大可以幫忙嗎...
發表人: Seachaos
積分: 2432
發表時間: 2011-07-25 14:26:45
您好
這段Code可以參考一下



[sea:javaCode]

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

URL url = new URL(sendURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

con.setRequestMethod("POST");

// 這邊要加上,不然File就只會當作POST
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

DataOutputStream ds = new DataOutputStream(con.getOutputStream());

...

// file post
for(int ax=0;ax<files.size();ax++){
ds.writeBytes(twoHyphens+boundary+end);
ds.writeBytes("Content-Disposition: form-data;");
ds.writeBytes("name=\""+inputName+"\";filename=\""+fileName+"\""+end+end);
int bufferSize = 1024;
int length = -1;
byte[] buffer = new byte[bufferSize];
FileInputStream fStream = new FileInputStream(reallyFilePath);
while((length = fStream.read(buffer))!=-1){
ds.write(buffer,0,length);
}
ds.writeBytes(end);
}
ds.writeBytes(twoHyphens+boundary+twoHyphens+end);
// close stream
ds.flush();

[/sea]
有問題的話歡迎一起討論
發表人: ooaaa
積分: 158
發表時間: 2011-07-25 19:38:40
我也是上網找到類似這樣的範例....
http://blog.csdn.net/xzk0532/article/details/6320808

大概像上面的...

我用上面的程式碼...我只能傳
private String uploadfile; 但我要傳的是照片...

如何做到點擊sd卡裡面的照片 就能傳送到我所以要位置呢??? 大大知道嗎??
發表人: Seachaos
積分: 2432
發表時間: 2011-07-27 01:40:51
這一段可以開起內建的Image Picker

[sea:javaCode]
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
[/sea]

參考看看
發表人: ooaaa
積分: 158
發表時間: 2011-07-27 02:26:33
我是做到按的....

問題:
做不到按完以後上傳到我要的位置...比如是 10.0.2.2/ABC/PHOTO 這樣...我應該按完之後 怎樣給他發送到我要的位置呢??
發表人: Seachaos
積分: 2432
發表時間: 2011-07-28 03:02:20
就是修改Fileinput的path

例如你說的
private String uploadfile
把他改成檔案路徑就可以了
發表人: ooaaa
積分: 158
發表時間: 2011-07-28 16:24:45
首先謝謝大大再次回應

可以請教大大另一個問題嗎....

其實 ANDROID <-> PHP <-> MYSQL
跟 ANDROID <->SQL(自己的)
跟 ANDROID <->server 他們的分別主要在哪??
發表人: Seachaos
積分: 2432
發表時間: 2011-07-28 22:20:57
不太清楚你的意思
你的SQL是說SQLite嗎?
SQLite是Android內建的一種儲存方式
好處是不用網路就可以簡單的存資料,但程式被移除資料也會不見

而Server的話就是透過Internet放資料...
有很多不同的方式,看是透過PHP->MySQL,或是另外的資料交換方式
發表人: ooaaa
積分: 158
發表時間: 2011-07-29 18:07:32
對 是sqllite 謝謝大大
發表人: ooaaa
積分: 158
發表時間: 2011-09-16 02:00:29
版大...不如可否給個完整的上傳照片範例呢...

因為我只能做到 設定名字的上傳 就是 要設定 sd卡裡的照片是aaa.jgp,

然後 flie = "/sd/aaa.jgp" 才可以上傳..但我要的是 我只要抓取sd卡裡的任意圖片

就可以跟名字一塊上傳到 mysql...請大大明示...感恩
發表人: Seachaos
積分: 2432
發表時間: 2011-09-17 10:55:05
[quote]ooaaa 提到:
版大...不如可否給個完整的上傳照片範例呢...

因為我只能做到 設定名字的上傳 就是 要設定 sd卡裡的照...[/quote]
試看看這個

[sea:javaCode]
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

URL url = new URL("http://192.168.11.51");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

con.setRequestMethod("POST");

DataOutputStream ds = new DataOutputStream(con.getOutputStream());

String
inputName = "類似Web中的<input name",
fileName = "在給Server的filename",
filePath = "檔案位置,如sdcard\123.jpg";

// 寫入檔案
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;");
ds.writeBytes("name=\"" + inputName + "\";filename=\"" + fileName
+ "\"" + end + end);
int bufferSize = 1024;
int length = -1;
byte[] buffer = new byte[bufferSize];
FileInputStream fStream = new FileInputStream(filePath);
while ((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);

// 結束
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
// close stream
ds.flush();
[/sea]
發表人: ooaaa
積分: 158
發表時間: 2011-09-17 13:51:41
可以給個php 範例嗎???,,,我在弄 filepath..發現也是同樣問題 只能上傳 我給定的名字...不知道是不是我的php問題
發表人: Seachaos
積分: 2432
發表時間: 2011-09-18 11:26:03
[quote]ooaaa 提到:
可以給個php 範例嗎???,,,我在弄 filepath..發現也是同樣問題 只能上傳 我給定的名字...不知道是...[/quote]
Opps, 忘了和你說要設定Connection的Header

以下是Android的上傳檔案範例
[sea:javaCode]
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

URL url = new URL("http://192.168.11.101/uploadTest/test.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

con.setRequestMethod("POST");

// 這邊要加上,不然File就只會當作POST
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

DataOutputStream ds = new DataOutputStream(con.getOutputStream());

String
inputName = "uploadFile",
fileName = "Image",
filePath = "/mnt/sdcard/_ExternalSD/DCIM/Camera/IMG_20100613_193802.jpg";

// 寫入檔案
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;");
ds.writeBytes("name=\"" + inputName + "\";filename=\"" + fileName
+ "\"" + end + end);
int bufferSize = 1024;
int length = -1;
byte[] buffer = new byte[bufferSize];
FileInputStream fStream = new FileInputStream(filePath);
while ((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);

// 結束
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
// close stream
ds.flush();
[/sea]


這邊是接收File的 (PHP)

[sea:javaCode]
if($_FILES['uploadFile']){
echo 'success';
// 剩下看你怎做
var_dump($_FILES['uploadFile']);
} else{
echo 'failed';
}
[/sea]
發表人: 訪客
發表時間: 2011-10-02 02:00:19
請問,我也想做到可以從sdcard上選擇任意圖片上傳到我的server....
原本檔案路徑寫死;sdcard/xxx.jpg可上傳
那現在要上傳sd卡裡的圖片
路徑應該怎麼改或是怎麼寫呢
謝謝...
發表人: 訪客
發表時間: 2011-10-02 13:09:07
fileName = "Image"


大大上傳完後發現檔名都是這個呢!

這樣上傳jpg,png後如何知道他的副檔名呢
發表人: Seachaos
積分: 2432
發表時間: 2011-10-02 14:47:12
Hi,
1. 你必需有選擇圖片的方法,看是用內建的圖片選擇器,或是自已寫都可以,只要有辦法取得要上傳的圖片檔名就可以了

2. 同上,在你取得圖片的時候只要把fileName改成你想上傳的圖片檔名就可以了
發表人: 訪客
發表時間: 2011-10-02 15:22:05
HI,Seachaos
我有把在SD卡裡面的圖片...做儲存的動作,並且做一個Dialg顯示出他的路徑...
是:content://media/external/images/meida/數字
這樣的一個路徑...
但當我把他當作絕對路徑使用的時候是錯的...
---------------以上是我邊想邊測試的-----------------------
那大大說,只要可以取得檔名?意思是,不是用路徑做為存取的動作嗎??
圖片選擇器是...?裡面的方法嗎...?

謝謝: )
發表人: Seachaos
積分: 2432
發表時間: 2011-10-02 17:57:32
您好,以下挑選圖片的範例,可以參考一下

[sea:javaCode]
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 0);
[/sea]

然後另外有個method是用來接收result的


[sea:javaCode]
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
// 其他
}
}
[/sea]
發表人: 訪客
發表時間: 2011-10-04 21:25:30
hi...想請問高手,
我在模擬器上輸入data(ex:string or int...)
偷過撰寫的php網頁做溝通再上傳到MySql...
我找到以下例子做測試可以成功,
private String uriAPI = "http://xxx.xxx.xxx.xx/xxx.php";
-----------------------------------------------------↓
...略)
private String sendPostDataToInternet(String strTxt)

{
/* 建立HTTP Post連線 */
HttpPost httpRequest = new HttpPost(uriAPI);
/*
* Post運作傳送變數必須用NameValuePair[]陣列儲存
*/
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("data", strTxt));
try
{
/* 發出HTTP request */
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* 取得HTTP response */
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
/* 若狀態碼為200 ok */
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
/* 取出回應字串 */
String strResult = EntityUtils.toString(httpResponse
.getEntity());
// 回傳回應字串
return strResult;
}


但是,當我要輸入兩個值的時候....該怎麼做呢...
嘗試了很多次....不是第第二個資料欄位無法輸入進去,就是第二個欄位資料沒有和第一筆資料在同一列...而是跑到資料表的下一筆去了,
懇請大家指點
謝謝
發表人: Seachaos
積分: 2432
發表時間: 2011-10-05 22:24:22
Hi:
如果要Post多個值的話Android端是
[sea:javaCode]
params.add(new BasicNameValuePair("data", "This is Vaue A"));
params.add(new BasicNameValuePair("data2", "This is Vaue B"));
params.add(new BasicNameValuePair("data3", "This is Vaue C"));
[/sea]

在PHP端的話是
[sea:javaCode]
echo $_POST['data']; // 會顯示 This is Value A
echo $_POST['data2']; // 會顯示 This is Value B
echo $_POST['data3']; // 會顯示 This is Value C
[/sea]