将图像从android发送到wamp服务器
我正在尝试将一个PNG图像从android设备发送到我笔记本电脑上运行的wamp服务器。以下是我用于AsyncTask的代码将图像从android发送到wamp服务器,android,Android,我正在尝试将一个PNG图像从android设备发送到我笔记本电脑上运行的wamp服务器。以下是我用于AsyncTask的代码 public class UploadImage extends AsyncTask<Void, Void, Void> { Bitmap image; String name; ArrayList<NameValuePair> dataToSend = new ArrayList();
public class UploadImage extends AsyncTask<Void, Void, Void> {
Bitmap image;
String name;
ArrayList<NameValuePair> dataToSend = new ArrayList();
Bitmap SendImg1;
public UploadImage(String rc){
int w,h;
name = rc;
try{
SendImg1 = BitmapFactory.decodeFile(name);
w = SendImg1.getHeight();
Integer i = new Integer(w);
String s = i.toString();
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "rc is " + rc,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "name is " + rc,Toast.LENGTH_SHORT).show();
}
catch(Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error in constructor",Toast.LENGTH_SHORT).show();
}
image = SendImg1;
}
@Override
protected Void doInBackground(Void... arg0) {
Toast.makeText(getApplicationContext(), "Inside OnPreEXEC" , Toast.LENGTH_SHORT).show();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
dataToSend.add(new BasicNameValuePair("image", encodedImage));
dataToSend.add(new BasicNameValuePair("name", name));
HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVERADDRESS + "/SavePicture.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
Toast.makeText(getApplicationContext(), "Inside try block of Onpostexec",Toast.LENGTH_SHORT).show();
client.execute(post);
Toast.makeText(getApplicationContext(), "Inside try block after client.execute(post) ",Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Problem in doinBG",Toast.LENGTH_SHORT).show();
}
return null;
}//doinBackground ends
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), "Inside onpost exec",Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
// code from BG starts here
// code from BG ends here
Toast.makeText(getApplicationContext(), "Image sent to the server",Toast.LENGTH_SHORT).show();
super.onPostExecute(result);
//Toast.makeText(getApplicationContext(), "Image Uploaded" , Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
他不来了。因此,我觉得我在这一行中可能出错了,但我无法确切地理解如何做?这段代码对测试它的许多事情都有影响。我建议你去掉一些东西。首先发布一个简单的字符串,并检查后端是否接收到它。完成后,添加一个字符串作为伪图像,然后检查图像的编码逻辑是否正常工作。不能在异步任务的doInBackground()中调用Toast()。你的应用程序将立即崩溃。但你没有报告撞车!将它们更改为Log.d()语句。在你这么做之后,这里的代码也改变了。明白了。。。问题是超时延迟…我只需要增加它。。。但是接收到一个零字节的空白图像。。可能出了什么问题
client.execute(post);