我想将图像上传到我的网络服务器。 我有服务器和所有的响应,所以这不是问题。 当我将以下代码粘贴到代码中时,它大多会崩溃。Android:处理图像时AsyncTask崩溃
我在调试器中看到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将在主要活动中创建) u 谢谢。
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。 和YES发布一些堆栈跟踪 – Yazan