PageRenderTime 68ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/sinonetwork/zhonghua/PublishQuestion.java

https://gitlab.com/zwp/Zhonghua
Java | 572 lines | 440 code | 71 blank | 61 comment | 72 complexity | e6d329f2060f5ba79700b81fb7f846c6 MD5 | raw file
  1. package com.sinonetwork.zhonghua;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import android.annotation.SuppressLint;
  9. import android.app.Activity;
  10. import android.app.ProgressDialog;
  11. import android.content.Intent;
  12. import android.graphics.Bitmap;
  13. import android.graphics.BitmapFactory;
  14. import android.graphics.drawable.ColorDrawable;
  15. import android.net.Uri;
  16. import android.os.Bundle;
  17. import android.os.Environment;
  18. import android.os.Handler;
  19. import android.os.Message;
  20. import android.provider.MediaStore;
  21. import android.view.Gravity;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.view.View.OnClickListener;
  25. import android.view.ViewGroup;
  26. import android.view.ViewGroup.LayoutParams;
  27. import android.view.WindowManager;
  28. import android.widget.AdapterView;
  29. import android.widget.AdapterView.OnItemClickListener;
  30. import android.widget.BaseAdapter;
  31. import android.widget.EditText;
  32. import android.widget.ImageView;
  33. import android.widget.ListView;
  34. import android.widget.PopupWindow;
  35. import android.widget.RelativeLayout;
  36. import android.widget.SimpleAdapter;
  37. import android.widget.TextView;
  38. import android.widget.Toast;
  39. import com.alibaba.fastjson.JSONObject;
  40. import com.lidroid.xutils.exception.HttpException;
  41. import com.lidroid.xutils.http.RequestParams;
  42. import com.lidroid.xutils.http.ResponseInfo;
  43. import com.lidroid.xutils.http.callback.RequestCallBack;
  44. import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
  45. import com.sinonetwork.zhonghua.model.Expert;
  46. import com.sinonetwork.zhonghua.utils.Bimp;
  47. import com.sinonetwork.zhonghua.utils.BitmapHelp;
  48. import com.sinonetwork.zhonghua.utils.Commons;
  49. import com.sinonetwork.zhonghua.utils.HttpHelp;
  50. import com.sinonetwork.zhonghua.utils.PrefUtil;
  51. import com.sinonetwork.zhonghua.utils.URLAddress;
  52. import com.sinonetwork.zhonghua.view.NoScrollGridView;
  53. public class PublishQuestion extends Activity implements OnClickListener {
  54. // private GridView publish_pic;
  55. private ImageView iv;
  56. private ListView listView;
  57. private SimpleAdapter adapter;
  58. // private PublishQuestionListAdapter listAdapter;
  59. private List<Map<String, Object>> list;
  60. private List<Map<String, Object>> list1;
  61. private ImageView back;
  62. private RelativeLayout choose_expert_img;
  63. private StringBuilder imgs = new StringBuilder();
  64. private Expert expert;
  65. private PopupWindow popUpWindow;
  66. private ImageView ivPublish;
  67. private EditText tvTitle;
  68. private EditText tvContent;
  69. private TextView choosed_expert_name;
  70. private ImageView choosed_expert_img;
  71. private ProgressDialog progressDialog;
  72. private MyAdapter mAdapter;
  73. private NoScrollGridView mGridView;
  74. private List<String> drr = new ArrayList<String>();
  75. Handler handler = new Handler() {
  76. public void handleMessage(Message msg) {
  77. switch (msg.what) {
  78. case 1:
  79. mAdapter.notifyDataSetChanged();
  80. break;
  81. }
  82. super.handleMessage(msg);
  83. }
  84. };
  85. @Override
  86. protected void onCreate(Bundle savedInstanceState) {
  87. super.onCreate(savedInstanceState);
  88. setContentView(R.layout.know_publish);
  89. getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
  90. list = new ArrayList<Map<String, Object>>();
  91. tvTitle = (EditText) findViewById(R.id.publish_title);
  92. tvContent = (EditText) findViewById(R.id.publish_content);
  93. back = (ImageView) findViewById(R.id.back);
  94. ivPublish = (ImageView) findViewById(R.id.publish_photo);
  95. choosed_expert_name = (TextView) findViewById(R.id.choosed_expert_name);
  96. choosed_expert_img = (ImageView) findViewById(R.id.choosed_expert_img);
  97. mGridView = (NoScrollGridView) findViewById(R.id.publish_pic);
  98. mAdapter = new MyAdapter();
  99. mAdapter.update();
  100. mGridView.setAdapter(mAdapter);
  101. mGridView.setOnItemClickListener(new OnItemClickListener() {
  102. @Override
  103. public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
  104. if (position == Bimp.bmp.size()) {
  105. // 调起相机
  106. showTakePhotoPopUpWindow(mGridView);
  107. }
  108. }
  109. });
  110. back.setOnClickListener(new OnClickListener() {
  111. @Override
  112. public void onClick(View v) {
  113. // TODO Auto-generated method stub
  114. Bimp.bmp.clear();
  115. Bimp.drr.clear();
  116. Bimp.max = 0;
  117. finish();
  118. }
  119. });
  120. choose_expert_img = (RelativeLayout) findViewById(R.id.choose_expert_img);
  121. choose_expert_img.setOnClickListener(new OnClickListener() {
  122. @Override
  123. public void onClick(View v) {
  124. // TODO Auto-generated method stub
  125. Intent intent = new Intent(PublishQuestion.this, ChooseExpert.class);
  126. startActivityForResult(intent, Commons.CHOOSE_EXPERT);
  127. }
  128. });
  129. ivPublish.setOnClickListener(this);
  130. // EventBus.getDefault().register(this);
  131. }
  132. @Override
  133. protected void onResume() {
  134. // TODO Auto-generated method stub
  135. super.onResume();
  136. mAdapter.update();
  137. }
  138. public void showPopupWindow(View view, View windowTokenView, boolean showAsDropDown) {
  139. if (popUpWindow == null) {
  140. popUpWindow = new PopupWindow();
  141. // 设置SelectPicPopupWindow的View
  142. popUpWindow.setContentView(view);
  143. // 设置SelectPicPopupWindow弹出窗体的宽
  144. if (showAsDropDown) {
  145. popUpWindow.setWidth(LayoutParams.WRAP_CONTENT);
  146. } else {
  147. popUpWindow.setWidth(LayoutParams.MATCH_PARENT);
  148. }
  149. // 设置SelectPicPopupWindow弹出窗体的高
  150. popUpWindow.setHeight(LayoutParams.WRAP_CONTENT);
  151. // 设置SelectPicPopupWindow弹出窗体可点击
  152. popUpWindow.setFocusable(true);
  153. if (!showAsDropDown) {
  154. // 设置SelectPicPopupWindow弹出窗体动画效果
  155. popUpWindow.setAnimationStyle(R.style.PopWindowAnim);
  156. }
  157. // 实例化一个ColorDrawable颜色为半透明
  158. ColorDrawable dw = new ColorDrawable(0xb0000000);
  159. // 设置SelectPicPopupWindow弹出窗体的背景
  160. popUpWindow.setBackgroundDrawable(dw);
  161. } else {
  162. popUpWindow.setContentView(view);
  163. }
  164. if (showAsDropDown) {
  165. popUpWindow.showAsDropDown(windowTokenView, 20, -120);
  166. } else {
  167. popUpWindow.showAtLocation(windowTokenView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
  168. }
  169. }
  170. public void closePopupWindow() {
  171. if (popUpWindow != null && popUpWindow.isShowing()) {
  172. popUpWindow.dismiss();
  173. }
  174. }
  175. /**
  176. * view初始化
  177. */
  178. public void showTakePhotoPopUpWindow(View windowTokenView) {
  179. View view = LayoutInflater.from(this).inflate(R.layout.select_photo_menu, null);
  180. showPopupWindow(view, windowTokenView, false);
  181. TextView takePhoto = (TextView) view.findViewById(R.id.take_photo);
  182. TextView selectFromAlbum = (TextView) view.findViewById(R.id.select_from_album);
  183. TextView cancel = (TextView) view.findViewById(R.id.cancel_photo);
  184. takePhoto.setOnClickListener(takePhotoButtonOnClickListener);
  185. selectFromAlbum.setOnClickListener(takePhotoButtonOnClickListener);
  186. cancel.setOnClickListener(takePhotoButtonOnClickListener);
  187. }
  188. private OnClickListener takePhotoButtonOnClickListener = new View.OnClickListener() {
  189. @Override
  190. public void onClick(View v) {
  191. switch (v.getId()) {
  192. case R.id.take_photo:
  193. String tempName = System.currentTimeMillis() + ".png";
  194. File file = getPickPhotoTmpFile(tempName);
  195. Commons.phoneName = file.getAbsolutePath();
  196. openCamera(file);
  197. closePopupWindow();
  198. break;
  199. case R.id.select_from_album:
  200. Intent intent = new Intent(PublishQuestion.this, ChoicePicActivity.class);
  201. startActivity(intent);
  202. closePopupWindow();
  203. break;
  204. case R.id.cancel_photo:
  205. closePopupWindow();
  206. break;
  207. }
  208. }
  209. };
  210. public File getPickPhotoTmpFile(String tmpFileName) {
  211. File root = this.getExternalFilesDir(null);
  212. if (root != null) {
  213. String absPath = root.getAbsolutePath();
  214. File tmpFile = new File(absPath, tmpFileName);
  215. return tmpFile;
  216. }
  217. return null;
  218. }
  219. /**
  220. * 打开相机
  221. *
  222. * @param file
  223. * 目标保存文件
  224. */
  225. public void openCamera(File file) {
  226. if (file == null) {
  227. return;
  228. }
  229. try {
  230. Uri uri = Uri.fromFile(file);
  231. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  232. intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  233. startActivityForResult(intent, Commons.REQUEST_CODE_CAMERA);
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. @Override
  239. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  240. super.onActivityResult(requestCode, resultCode, data);
  241. if (requestCode == Commons.CHOOSE_EXPERT && resultCode == RESULT_OK) {
  242. expert = (Expert) data.getSerializableExtra("expert");
  243. if (expert != null) {
  244. choosed_expert_name.setText(expert.getRealName());
  245. BitmapHelp.getBitmapUtils(this).display(choosed_expert_img, URLAddress.TPURL + expert.getPictures());
  246. }
  247. } else if (requestCode == Commons.REQUEST_CODE_CAMERA) {
  248. if (Bimp.drr.size() < 6 && resultCode == -1) {
  249. Bimp.drr.add(Commons.phoneName);
  250. mAdapter.notifyDataSetChanged();
  251. }
  252. }
  253. }
  254. // public void onEvent(TakePhotoOutputEvent event) {
  255. // if (event == null) {
  256. // return;
  257. // }
  258. //
  259. // if ((event.requestCode == Commons.REQUEST_CODE_ALBUM || event.requestCode
  260. // == Commons.REQUEST_CODE_CAMERA) && event.resultCode ==
  261. // Activity.RESULT_OK) { // 相册
  262. // File file = new File(event.filePath);
  263. // File newFile = null;
  264. // try {
  265. // newFile =
  266. // BitmapHelp.saveBitmapFile(BitmapHelp.revitionImageSize(file.getAbsolutePath()),
  267. // Environment.getExternalStorageDirectory().getPath() + "/zhonghua/" +
  268. // System.currentTimeMillis() + ".jpg");
  269. // } catch (IOException e) {
  270. //
  271. // e.printStackTrace();
  272. // }
  273. //
  274. // if (iv == iv1) {
  275. // mDatas.put("iv1", newFile);
  276. // } else if (iv == iv2) {
  277. // mDatas.put("iv2", newFile);
  278. // } else if (iv == iv3) {
  279. // mDatas.put("iv3", newFile);
  280. // } else if (iv == iv4) {
  281. // mDatas.put("iv4", newFile);
  282. // } else if (iv == iv5) {
  283. // mDatas.put("iv5", newFile);
  284. // } else if (iv == iv6) {
  285. // mDatas.put("iv6", newFile);
  286. // }
  287. // BitmapHelp.getBitmapUtils(this).display(iv, newFile.getAbsolutePath());
  288. // // iv.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
  289. //
  290. // }
  291. // }
  292. @Override
  293. public void onClick(View v) {
  294. switch (v.getId()) {
  295. case R.id.publish_photo:
  296. if (tvTitle.getText().toString().equals("")) {
  297. Toast.makeText(this, "请填写问题标题", 2000).show();
  298. return;
  299. }
  300. if (tvContent.getText().toString().equals("")) {
  301. Toast.makeText(this, "请填写问题内容", 2000).show();
  302. return;
  303. }
  304. if (drr.size() != 0) {
  305. Iterator<String> iterators = drr.iterator();
  306. while (iterators.hasNext()) {
  307. final String key = iterators.next();
  308. RequestParams params = new RequestParams();
  309. params.addBodyParameter("method", "upimg");
  310. File file = new File(key);
  311. params.addBodyParameter("upload", file);
  312. uploadImg(params);
  313. }
  314. } else {
  315. if (progressDialog == null) {
  316. showProgressDialog("问题发布中...");
  317. }
  318. AddQuestion();
  319. }
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. public void uploadImg(RequestParams params) {
  326. if (progressDialog == null) {
  327. showProgressDialog("问题发布中...");
  328. }
  329. HttpHelp.getInstance().send(HttpMethod.POST, URLAddress.TPURLER + "upimg.action", params, new RequestCallBack<String>() {
  330. @Override
  331. public void onFailure(HttpException arg0, String arg1) {
  332. closeProgressDialog();
  333. Toast.makeText(PublishQuestion.this, "发布问题失败", 2000).show();
  334. }
  335. @Override
  336. public void onSuccess(ResponseInfo<String> responseInfo) {
  337. JSONObject jsonObject = JSONObject.parseObject(responseInfo.result);
  338. if (jsonObject.getString("resultcode").equals("ok")) {
  339. imgs.append(jsonObject.getString("resultdata")).append(",");
  340. int length = imgs.toString().split(",").length;
  341. if (length == (Bimp.bmp.size())) {
  342. AddQuestion();
  343. }
  344. }
  345. }
  346. });
  347. }
  348. public void AddQuestion() {
  349. RequestParams params = new RequestParams();
  350. params.addBodyParameter("method", "addQuestion");
  351. params.addBodyParameter("questionText", tvContent.getText().toString());
  352. params.addBodyParameter("questionTitle", tvTitle.getText().toString());
  353. params.addBodyParameter("userName", PrefUtil.getStringPref(this, "zhonghua_userName"));
  354. if (expert != null) {
  355. params.addBodyParameter("expertName", expert.getUserName());
  356. } else {
  357. params.addBodyParameter("expertName", "");
  358. }
  359. params.addBodyParameter("region", "4521984");
  360. if (imgs.length() != 0) {
  361. String picutres = imgs.toString().substring(0, imgs.length() - 1);
  362. params.addBodyParameter("pictures", picutres);
  363. }
  364. HttpHelp.getInstance().send(HttpMethod.POST, URLAddress.URLER, params, new RequestCallBack<String>() {
  365. @Override
  366. public void onFailure(HttpException arg0, String arg1) {
  367. closeProgressDialog();
  368. Toast.makeText(PublishQuestion.this, "发布问题失败", 2000).show();
  369. }
  370. @SuppressLint("ShowToast")
  371. @Override
  372. public void onSuccess(ResponseInfo<String> responseInfo) {
  373. closeProgressDialog();
  374. JSONObject jsonObject = JSONObject.parseObject(responseInfo.result);
  375. if (jsonObject.getString("resultcode").equals("ok")) {
  376. imgs.setLength(0);
  377. Toast.makeText(PublishQuestion.this, "问题发布成功", 2000).show();
  378. Bimp.bmp.clear();
  379. Bimp.drr.clear();
  380. Bimp.max = 0;
  381. PublishQuestion.this.finish();
  382. }
  383. }
  384. });
  385. }
  386. public void showProgressDialog(String message) {
  387. if (progressDialog == null) {
  388. progressDialog = new ProgressDialog(this);
  389. progressDialog.setCancelable(false);
  390. }
  391. if (message == null) {
  392. progressDialog.setMessage("数据加载中...");
  393. } else {
  394. progressDialog.setMessage(message);
  395. }
  396. progressDialog.show();
  397. }
  398. public void closeProgressDialog() {
  399. if (progressDialog != null && progressDialog.isShowing()) {
  400. progressDialog.dismiss();
  401. progressDialog = null;
  402. }
  403. }
  404. public class MyAdapter extends BaseAdapter {
  405. LayoutInflater inflater = LayoutInflater.from(PublishQuestion.this);
  406. private int selectedPosition = -1;// 选中的位置
  407. private boolean shape;
  408. public boolean isShape() {
  409. return shape;
  410. }
  411. public void setShape(boolean shape) {
  412. this.shape = shape;
  413. }
  414. public void update() {
  415. loading();
  416. }
  417. @Override
  418. public int getCount() {
  419. return (Bimp.bmp.size() + 1);
  420. }
  421. @Override
  422. public Object getItem(int position) {
  423. return null;
  424. }
  425. @Override
  426. public long getItemId(int position) {
  427. return position;
  428. }
  429. public void setSelectedPosition(int position) {
  430. selectedPosition = position;
  431. }
  432. public int getSelectedPosition() {
  433. return selectedPosition;
  434. }
  435. @Override
  436. public View getView(int position, View convertView, ViewGroup parent) {
  437. ViewHolder holder = null;
  438. if (convertView == null) {
  439. convertView = inflater.inflate(R.layout.pictures_publish_photoitem, parent, false);
  440. holder = new ViewHolder();
  441. holder.image = (ImageView) convertView.findViewById(R.id.iv_photo);
  442. convertView.setTag(holder);
  443. } else {
  444. holder = (ViewHolder) convertView.getTag();
  445. }
  446. if (position == Bimp.bmp.size()) {
  447. holder.image.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.phone_icon));
  448. if (position == 6) {
  449. holder.image.setVisibility(View.GONE);
  450. }
  451. } else {
  452. holder.image.setImageBitmap(Bimp.bmp.get(position));
  453. }
  454. return convertView;
  455. }
  456. public class ViewHolder {
  457. public ImageView image;
  458. }
  459. Handler handler = new Handler() {
  460. public void handleMessage(Message msg) {
  461. switch (msg.what) {
  462. case 1:
  463. mAdapter.notifyDataSetChanged();
  464. break;
  465. }
  466. super.handleMessage(msg);
  467. }
  468. };
  469. public void loading() {
  470. new Thread(new Runnable() {
  471. public void run() {
  472. while (true) {
  473. if (Bimp.max == Bimp.drr.size()) {
  474. Message message = new Message();
  475. message.what = 1;
  476. handler.sendMessage(message);
  477. break;
  478. } else {
  479. try {
  480. String path = Bimp.drr.get(Bimp.max);
  481. System.out.println(path);
  482. Bitmap bm = Bimp.revitionImageSize(path);
  483. bm = Bimp.rotateBitmap(Bimp.readPicDegree(path), bm);
  484. Bimp.bmp.add(bm);
  485. String newStr = path.substring(path.lastIndexOf("/"));
  486. drr.add(Environment.getExternalStorageDirectory().getPath() + "/Zhonghua/images/" + newStr);
  487. Bimp.saveBitmap(bm, "" + newStr);
  488. Bimp.max += 1;
  489. Message message = new Message();
  490. message.what = 1;
  491. handler.sendMessage(message);
  492. } catch (IOException e) {
  493. e.printStackTrace();
  494. }
  495. }
  496. }
  497. }
  498. }).start();
  499. }
  500. }
  501. }