我想将图片上传到我的网络服务器。 我有来自服务器和所有的响应,所以这不是问题。 当我将以下行粘贴到代码中时,它主要崩溃。
我在调试器中看到了AsyncTask的RuntimeException,这就是全部。
另一个问题:我可以将位图解压缩为字节数组,因此我可以使用原始文件结尾上传原始文件吗?
以下是问题专栏:
// create the image for the server
Bitmap bm=BitmapFactory.decodeFile(Values.imageFilePath);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byte_array=stream.toByteArray();
String byteString=Base64.encodeToString(byte_array, Base64.DEFAULT);
这是整个asynctask类,上面的代码被注释掉并运行。 有什么问题? (ProgressDialog将在主要活动中创建) ü 感谢。
class UploadThread extends AsyncTask<String, Void, String>
{
private Context _context;
private ProgressDialog pd;
public UploadThread(Context context)
{
super();
_context=context;
}
protected String doInBackground(String... urls)
{
String response="Android says \"Nothing happened.\"";
InputStream is = null;
StringBuilder sb=null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Values.hostname);
ArrayList<NameValuePair> nvp=new ArrayList<NameValuePair>();
// This is the Post data.
nvp.add(new BasicNameValuePair("plog_android_upload","plogupload"));
nvp.add(new BasicNameValuePair("thecomment",Values.comment));
// get the image and process it.
// Values.progressDialog.setMessage("Processing image..");
// create the image for the server
// Bitmap bm=BitmapFactory.decodeFile(Values.imageFilePath);
// ByteArrayOutputStream stream=new ByteArrayOutputStream();
// bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// byte[] byte_array=stream.toByteArray();
// String byteString=Base64.encodeToString(byte_array, Base64.DEFAULT);
//Values.progressDialog.setMessage("Uploading..");
//nvp.add(new BasicNameValuePair("upload_file",byteString));
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity entity = httpresponse.getEntity();
is = entity.getContent();
response="Connection established.";
}catch(UnknownHostException eu){ // gets thrown on unknown host.
return "No connection!\n(or wrong hostname)";
}catch(HttpHostConnectException ex){ // gets thrown on unknown connection.
return "No connection!\n(or wrong hostname)";
}catch(Exception e){
return "Error in http connection:"+e.toString();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response="Server says: "+sb.toString();
}catch(Exception e){
response= "Error converting HTTP result: "+e.toString();
return response;
}
return response;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
// dismiss the progress dialog.
Values.progressDialog.dismiss();
// Show the user some feedback.
Toast.makeText(_context,result, Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
我认为您使用的代码
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Values.hostname);
ArrayList<NameValuePair> nvp=new ArrayList<NameValuePair>();
// This is the Post data.
nvp.add(new BasicNameValuePair("plog_android_upload","plogupload"));
nvp.add(new BasicNameValuePair("thecomment",Values.comment));
不应该在doInBackground中执行,当我不得不在doInBackground中使用arrayList时遇到了同样的问题,所以我将那部分代码更改为onPreExecute(),它在UIThread中工作