问候,
我在从Android手机上传照片时遇到问题。这是我用来提示用户选择照片的代码:
public class Uploader{
public void upload(){
Log.i("EOH","hi...");
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
//what do do now with this Bitmap...
//how do I upload it?
}
}
}
所以我有位图b,但我不知道下一步该做什么?
我有以下用于发送POST请求的代码:
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("someValA", String.valueOf(lat)));
params.add(new BasicNameValuePair("someValB", String.valueOf(lng)));
new HttpConnection(handler).post("http://myurl.com/upload.php",params);
如何将Bitmap图像连接到此?我已经搜索谷歌多年了,找不到一个好方法。
我希望有人可以提供帮助。
非常感谢,
好的,我尝试过chirag shah的建议。这是我的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
Uri imageUri=data.getData();
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("image", imageUri.getPath()));
post("http://www.myurl.com/ajax/uploadPhoto.php",params);
}
}
其中post函数(推荐)是:
public void post(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
// If the key equals to "image", we use FileBody to transfer the data
entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
}else{
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
Log.i("EOH",response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
但是myurl.com/ajax/uploadPhoto.php什么也没得到。我已经创建了一个PHP日志来记录uploadPhoto.php上的任何活动,并且没有任何显示。值得注意的是,如果我只是将myurl.com/ajax/uploadPhoto.php直接输入网络浏览器,它就可以正常登录。
还有其他建议吗?
非常感谢,
答案 0 :(得分:0)
如果你还没有。尝试将帖子包装到异步任务中的服务器并调用该任务。我发现我使用的api需要远离主线程的http posts / gets,以避免锁定主线程的可能问题。
将它设置为异步任务并开始实际工作相对容易。
有关异步任务的更多信息:http://developer.android.com/reference/android/os/AsyncTask.html
对于一个简单的例子,这是我的异步任务之一:
private class SomeAsyncTask extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... arg0) {
UserFunctions uf = new UserFunctions();
JSONObject res = uf.doPostToServer(arg0[0], arg0[1], arg0[2], getApplicationContext());
return res;
}
@Override
protected void onPostExecute(JSONObject result) {
try {
int success = result.getInt("success");
if(success == 1) {
Intent vcact = new Intent(getApplicationContext(), OtherActivity.class);
vcact.putExtra("id", cid);
startActivity(vcact);
// close main screen
finish();
} else {
Toast.makeText(SomeActivity.this, "Error saving to server. Try again.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
并使用
调用onclicknew SomeAsyncTask().execute(cid, otherdata, moredata);