【发布时间】:2014-05-07 11:31:13
【问题描述】:
我想上传一张图片到我的网络服务器。 我有来自服务器的响应,所以这不是问题。 当我将以下行粘贴到代码中时,它大多会崩溃。
我在调试器中看到 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);
这是整个异步任务类,上面的代码被注释掉并运行。 有什么问题? (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();
}
}
【问题讨论】:
-
发布堆栈跟踪以及确切的行号。
-
你的问题在这里 Values.progressDialog.setMessage("Uploading..");因为这是 UI,从 doInBackground 无法触及 UI。并且是发布一些堆栈跟踪
标签: android image upload android-asynctask