/** * ʧ�����췢������ * created by songdebin * * ***/ package com.bdyjy.fragment; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.MediaStore; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.alibaba.fastjson.JSON; import com.bdyjy.R; import com.bdyjy.activity.MainActivity; import com.bdyjy.constants.Const; import com.bdyjy.constants.HandlerOrder; import com.bdyjy.entity.ReleaseBean; import com.bdyjy.entity.upload.ImgUploadResultBean; import com.bdyjy.util.OkHttpUtils; import com.bdyjy.util.SPUtils; public class LostFindReleaseFragment extends Fragment { private MainActivity ctx; private TextView tv_back; private Button btn_my; private Button btn_commit; Handler handler = null; // ������ʾ״̬֮����л� private RelativeLayout l1; private RelativeLayout l2; private String imagePath1 = null; private String imagePath2 = null; private ImageView addimage1; private ImageView addimage2; private RelativeLayout lv_addImage; private TextView tv_tag1; private TextView tv_tag2; private View view_tag1; private View view_tag2; private EditText et_title; private EditText et_contacts; private EditText et_tel; private int type = 2; private EditText et_description; private int source; private List<ImgUploadResultBean> uploadResultList = new ArrayList<ImgUploadResultBean>(); private Map<String, String> imgsMap = new HashMap<String, String>(); public LostFindReleaseFragment(MainActivity ctx, int source) { this.ctx = ctx; this.source = source; } String toastMsg = null; private void initHandler() { handler = new Handler(ctx.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case HandlerOrder.TOAST: // TODO Toast.makeText(ctx, toastMsg, Toast.LENGTH_LONG).show(); break; case HandlerOrder.TO_MAIN: //btn_my.performClick(); break; case HandlerOrder.PROCESSBAR_SHOW: //���ȹ����� ctx.showRoundProcessDialog(); break; case HandlerOrder.PROCESSBAR_HIDE://���ȹ����� ctx.hideRoundProcessDialog(); break; case HandlerOrder.UPLOAD_OK: //ͼƬ�ϴ� �ɹ� ctx.hideRoundProcessDialog(); String result = msg.getData().get("body").toString(); System.out.println("result:\n" + result); ImgUploadResultBean is = JSON.parseObject(result, ImgUploadResultBean.class); if ("0".equals(is.getApp_result_key())) { System.out.println(is.getAttachmentName());// �ļ��� System.out.println(is.getAttachmentUrl());// �ڷ������ϵĴ洢·�� System.out.println(is.getPrix());// �ļ���������ַ System.out.println(is.getType());// �ļ���� System.out.println(is.getSize());// �ļ���С uploadResultList.add(is); } else { Toast.makeText(ctx, "�ϴ�����App_result_key��=0", Toast.LENGTH_LONG).show(); } if (uploadResultList.size() == imgsMap.size())// �������ͼƬ���Ѿ��ϴ���� ��ʼ�ϴ�������Ϣ { Commit(); } break; case HandlerOrder.POST_OK: //��Ϣ�ϴ��ɹ� ctx.hideRoundProcessDialog(); String res; res=msg.getData().getString("body"); ReleaseBean bean = JSON.parseObject(res,ReleaseBean.class); String app_result_key = bean.getApp_result_key(); if ("0".equals(app_result_key)) //���ص���Ϣ { toastMsg = "��Ϣ�ύ�ɹ�"; handler.sendEmptyMessage(HandlerOrder.TOAST); //�Զ���ת���ֽ����б� if (source == Const.FRAGMENT2_ID) { ctx.jumpToClickById(Const.FRAGMENT_LOSTFINDLIST_ID); } else { ctx.jumpToMyLost(source); } }else { toastMsg = "��Ϣ�ύʧ�ܣ��������ݽ����ں���Ӣ���Լ�������"; handler.sendEmptyMessage(HandlerOrder.TOAST); } break; case HandlerOrder.POST_ERROR: //��Ϣ�ϴ��ɹ� toastMsg = "��Ϣ�ύʧ�ܣ������������"; handler.sendEmptyMessage(HandlerOrder.TOAST); ctx.hideRoundProcessDialog(); break; case HandlerOrder.UPLOAD_ERROR: toastMsg = "��Ϣ�ύʧ�ܣ������������"; handler.sendEmptyMessage(HandlerOrder.TOAST); ctx.hideRoundProcessDialog(); break; } } }; } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.lost_release_fragment, null); initHandler(); // ��������ؼ��ij�ʼ�� et_title = (EditText) view.findViewById(R.id.et_title_lostfind_release); et_contacts = (EditText) view .findViewById(R.id.et_contacts_lostfind_release); et_tel = (EditText) view.findViewById(R.id.et_tel_lostfind_release); et_description = (EditText) view .findViewById(R.id.et_description_lostfind_release); // ������һ���� ʧ�������б� tv_back = (TextView) view.findViewById(R.id.tv_back); tv_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (source == Const.FRAGMENT2_ID) { ctx.jumpToClickById(Const.FRAGMENT_LOSTFINDLIST_ID); } else { ctx.jumpToMyLost(source); } } }); addimage1 = (ImageView) view .findViewById(R.id.iv_show1_lostfind_release); addimage2 = (ImageView) view .findViewById(R.id.iv_show2_lostfind_release); // ���ͼƬ lv_addImage = (RelativeLayout) view .findViewById(R.id.lv_addimage_lostfind_release); lv_addImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, 200); } }); if (source == Const.FRAGMENT2_ID) { // ��ת���ҵ�ʧ������ btn_my = (Button) view.findViewById(R.id.setting); btn_my.setVisibility(View.VISIBLE); btn_my.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ctx.jumpToMyLost(Const.FRAGMENT2_ID); } }); } else { } // ���������ύ�İ��� btn_commit = (Button) view.findViewById(R.id.btn_lostfind_release); btn_commit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String title = et_title.getText().toString().trim(); String tel = et_tel.getText().toString().trim(); String contacts = et_contacts.getText().toString().trim(); String description = et_description.getText().toString().trim(); if(TextUtils.isEmpty(title)){ toastMsg = "����д����"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else if(TextUtils.isEmpty(tel)){ toastMsg = "����д�����ϵ��ʽ"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else if(TextUtils.isEmpty(contacts)){ toastMsg = "����д��ϵ��"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else if(TextUtils.isEmpty(description)){ toastMsg = "����������"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else{ // �����ͼƬ���ϴ�ͼƬ if (!TextUtils.isEmpty(imgsMap.get(img1Key)) || !TextUtils.isEmpty(imgsMap.get(img2Key))) { uploadResultList.clear(); // ���ѡ����ͼƬ try { // TODO ������ύ�������ϴ�ͼƬ��ѭ�������ϴ��ķ��� for (String key : imgsMap.keySet()) { handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW); OkHttpUtils.getInstance().uploadImg(ctx, imgsMap.get(key), handler); //�ϴ�ͼƬ } } catch (Exception e) { e.printStackTrace(); } }else{// ���û��ͼƬֱ���ϴ�������Ϣ Commit(); } } } }); initTab(view); return view; } /** * ��ʼ��ѡ� * * @param view */ private void initTab(View view) { // ���������л� l1 = (RelativeLayout) view.findViewById(R.id.ly_lost1); l2 = (RelativeLayout) view.findViewById(R.id.ly_lost2); tv_tag1 = (TextView) view.findViewById(R.id.tv_lost_tag1); tv_tag2 = (TextView) view.findViewById(R.id.tv_lost_tag2); view_tag1 = (View) view.findViewById(R.id.view_lost_tag1); view_tag2 = (View) view.findViewById(R.id.view_lost_tag2); // ÿ���л��������ʺ�̨��Ȼ����ص��б��� l1.setOnClickListener(new OnClickListener() { @SuppressLint("NewApi") @Override public void onClick(View v) { // ���ҽ���ǰѡ�е�������Ӵֱ�ɫ�������Ļع�ԭstyle tv_tag1.setTextColor(0xFF5A5A5A); tv_tag2.setTextColor(0xFF9A9A9A); view_tag1.setBackgroundColor(0xFF5A5A5A); view_tag2.setBackgroundColor(0x005A5A5A); type = 2; } }); l2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // ���ҽ���ǰѡ�е�������Ӵֱ�ɫ�������Ļع�ԭstyle tv_tag1.setTextColor(0xFF9A9A9A); tv_tag2.setTextColor(0xFF5A5A5A); view_tag1.setBackgroundColor(0x005A5A5A); view_tag2.setBackgroundColor(0xFF5A5A5A); type = 1; } }); } /** * *�ύ���ݰ���ͼƬ����Ϣ * songdebin * */ public void Commit(){ new Thread() //�����ϴ�������Ϣ { @Override public void run() { String res = null; // ��sharePreference��ȡ��֮ǰ�洢�IJ��� String token = (String) SPUtils.get(ctx, "token", ""); String singnature = (String) SPUtils.get(ctx, "singnature", ""); String st = (String) SPUtils.get(ctx, "st", ""); String title = et_title.getText().toString().trim(); String tel = et_tel.getText().toString().trim(); String contacts = et_contacts.getText().toString().trim(); String description = et_description.getText().toString().trim(); // ����post���� tryPost(title, tel, contacts,type, description, token, singnature, st); } }.start(); } private void tryPost(String title, String tel,String contacts,int type, String description, String token, String singnature, String st) { // ����ʹ���첽post������� HashMap<String, String> map = new HashMap<String, String>(); // ���ڿ�ʼ������� map.put("title", title); map.put("tel", tel); //map.put("contacts",contacts); map.put("description", description); for (int i = 0; i < uploadResultList.size(); i++) //ͼƬ��Ϣ { map.put("attArry[" + i + "].filePath", uploadResultList.get(i) .getAttachmentUrl()); map.put("attArry[" + i + "].fileName", uploadResultList.get(i) .getAttachmentName()); map.put("attArry[" + i + "].fileSize", uploadResultList.get(i) .getSize()); map.put("attArry[" + i + "].fileType", uploadResultList.get(i) .getType()); } String http = "/admin/lostfound/addSave.do?token=" + token //******************************·��û������ + "&singnature=" + singnature + "&st=" + st+"&type="+type; handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW); OkHttpUtils.getInstance().doPostAsync(ctx, http, map, handler); System.out.println("�ύ��������"+http+" "+map); } // �ӱ��ػ�ȡͼƬ @SuppressLint("DefaultLocale") @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 200 && resultCode == Activity.RESULT_OK) { Log.d("dd", data.toString()); Uri uri = data.getData(); // ���صĽ�� ContentResolver cr = ctx.getContentResolver(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = ctx.getContentResolver().query(uri, filePathColumn, null, null, null); if (cursor == null) { System.out.print("cursor :" + null); return; } System.out.print("cursor :" + cursor); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); if (!picturePath.toLowerCase().endsWith(".jpg") && !picturePath.toLowerCase().endsWith(".jpeg")) { Toast.makeText(ctx.getApplicationContext(), "��ѡ��jpg��ʽ��ͼƬ", Toast.LENGTH_SHORT).show(); return; } try { //Toast.makeText(ctx, picturePath, Toast.LENGTH_SHORT).show(); if (picIndex == 1) { imgsMap.put(img1Key, picturePath); imagePath1=picturePath; ShowImg(imagePath1, addimage1); picIndex = 2; }else if(picIndex==2){ imagePath2=picturePath; imgsMap.put(img2Key, picturePath); ShowImg(imagePath2, addimage2); picIndex = 1; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // images.add(picturePath);// ������Ϊ�˻�ȡͼƬ��·�� // setAdapter(new MyAdapter(UploadActivity.this)); Log.d("dd", picturePath); } } private int picIndex = 1; private String img1Key = "img1"; private String img2Key = "img2"; public void ShowImg(String uri, ImageView headimage2) throws IOException { FileInputStream fs = new FileInputStream(uri); BufferedInputStream bs = new BufferedInputStream(fs); Bitmap btp = BitmapFactory.decodeStream(bs); headimage2.setImageBitmap(btp); bs.close(); fs.close(); btp = null; } /* * (non-Javadoc) * fragment ����ʱ���� * @see android.app.Fragment#onDestroy() */ @Override public void onDestroy() { InputMethodManager imm = (InputMethodManager) ctx .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(null, 0); super.onDestroy(); } }