package com.bdyjy.fragment;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
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.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.ViewGroup;
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.HandlerOrder;
import com.bdyjy.entity.personalinfo.PersonalInfoContentQueryResultBean;
import com.bdyjy.entity.upload.ImgUploadResultBean;
import com.bdyjy.util.OkHttpUtils;
import com.bdyjy.util.RoundImageView;
import com.bdyjy.util.SPUtils;
/**
* holy������Ϣ��ҳ��
*
* @author
*/
@SuppressLint("NewApi")
public class PersonalInfoModifyFragment extends Fragment
{
private TextView tv_back;
private RoundImageView headimage;
private TextView userName;
private String mUserName;
private TextView identifierNumber;
private EditText email;
private String mEmail;
private TextView college;
private TextView major;
private EditText tel;
private String mTel;
private RelativeLayout submit;
private String userImg = "";
private String imagePath1 = null;
private String imagePath2 = null;
private ImageView addimage1;
private ImageView addimage2;
private Map<String, String> imgsMap = new HashMap<String, String>();
private List<ImgUploadResultBean> uploadResultList = new ArrayList<ImgUploadResultBean>();
private Handler handler;
private String toastMsg;
private MainActivity ctx;
private void initHandler()
{
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case HandlerOrder.TOAST:
// TODO
Toast.makeText(ctx, toastMsg, Toast.LENGTH_LONG).show();
break;
case HandlerOrder.UPDATE_LISTVIEW:
// listView1.onLoad();
// loadData();
break;
case HandlerOrder.PROCESSBAR_SHOW:
ctx.showRoundProcessDialog();
break;
case HandlerOrder.PROCESSBAR_HIDE:
ctx.hideRoundProcessDialog();
break;
case HandlerOrder.UPLOAD_ERROR:
Toast.makeText(ctx, "�ϴ������������쳣", Toast.LENGTH_LONG).show();
ctx.hideRoundProcessDialog();
break;
case HandlerOrder.UPLOAD_OK:
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);
userImg = is.getAttachmentUrl();
} else
{
Toast.makeText(ctx, "�ϴ�����App_result_key��=0",
Toast.LENGTH_LONG).show();
}
if (uploadResultList.size() == imgsMap.size())// �������ͼƬ���Ѿ��ϴ����
{
InfoCommit();
}
break;
case HandlerOrder.POST_OK:
// ���������post֮��Ľ��
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE);
// ����ֵ������JSON��ʽ�����ݣ���Ҫ���������
String res = msg.getData().get("body").toString();
System.out.println("post_result:\n" + res);
PersonalInfoContentQueryResultBean bean = JSON.parseObject(
res, PersonalInfoContentQueryResultBean.class);
String app_result_key = bean.getApp_result_key();
if ("0".equals(app_result_key))
{
toastMsg = "��Ϣ�ύ�ɹ�";
handler.sendEmptyMessage(HandlerOrder.TOAST);
// handler.sendEmptyMessage(HandlerOrder.TO_MAIN);
// ctx.jumpToMyComplaintListFregment();
ctx.jumpToPersonalCenterFragment();
} else
{
toastMsg = bean.getApp_result_message_key();// "��Ϣ�ύʧ�ܣ�������";
handler.sendEmptyMessage(HandlerOrder.TOAST);
}
break;
}
}
};
}
public PersonalInfoModifyFragment(MainActivity ctx)
{
this.ctx = ctx;
initHandler();
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.personal_info_modify_fragment,
null);
tv_back = (TextView) view.findViewById(R.id.tv_back);
tv_back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
ctx.jumpToPersonalInfoFragment();
}
});
// ������Ϣ������
headimage = (RoundImageView) view.findViewById(R.id.headimage);
String ur1 = MainActivity.personalInfo.getAttachmentPrefix();
String ur2 = MainActivity.personalInfo.getUserImg();
// TODO
headimage.setImageUrl(ur1 + ur2);
userName = (TextView) view.findViewById(R.id.username);
userName.setText(MainActivity.personalInfo.getUserName());
identifierNumber = (TextView) view.findViewById(R.id.identifierNumber);
identifierNumber.setText(MainActivity.personalInfo
.getIdentifierNumber());
email = (EditText) view.findViewById(R.id.email);
email.setText(MainActivity.personalInfo.getEmail());
college = (TextView) view.findViewById(R.id.college);
college.setText(MainActivity.personalInfo.getCollege());
major = (TextView) view.findViewById(R.id.major);
major.setText(MainActivity.personalInfo.getMajor());
tel = (EditText) view.findViewById(R.id.tel);
tel.setText(MainActivity.personalInfo.getMobile());
headimage = (RoundImageView) view.findViewById(R.id.headimage);
headimage.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, 1);
}
});
submit = (RelativeLayout) view.findViewById(R.id.info_submit);
submit.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
mUserName = userName.getText().toString();
mEmail = email.getText().toString();
mTel = tel.getText().toString();
// Toast.makeText(ctx, mUserName+mEmail+mTel,
// Toast.LENGTH_SHORT).show();
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
{
// ���û��ѡ��ͼƬ��ֱ���ύ
InfoCommit();
}
}
});
return view;
}
private int picIndex = 1;
private String img1Key = "img1";
private String img2Key = "img2";
@SuppressLint("DefaultLocale")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && 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();
ShowImg(picturePath, headimage);
imgsMap.put(img1Key, picturePath);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// images.add(picturePath);// ������Ϊ�˻�ȡͼƬ��·��
// setAdapter(new MyAdapter(UploadActivity.this));
Log.d("dd", picturePath);
}
}
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;
}
private void InfoCommit()
{
new Thread()
{
@Override
public void run()
{
System.out.println("����Ϣ�ύ��������:" + userImg + mUserName + mEmail
+ mTel);
// ȡ��ҳ���ϵIJ����Լ�Ӧ���Ѿ��洢�IJ���
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", "");
// ����post����
tryPost(userImg, mUserName, mEmail, mTel, token, singnature, st);
}
}.start();
}
/**
* �����첽post��ʽ�����ύ
*/
private void tryPost(String userImg, String mUserName, String mEmail,
String mTel, String token, String singnature, String st)
{
// ����ʹ���첽post�������
HashMap<String, String> map = new HashMap<String, String>();
// ���ڿ�ʼ�������(��ȡ)
map.put("userName", mUserName);
// map.put("tel", mTel);
// map.put("mobile","123");
map.put("tel", "123");
map.put("mobile", mTel);
map.put("email", mEmail);
map.put("userImg", userImg);
// ���ڿ�ʼ�������(�����)
map.put("college", MainActivity.personalInfo.getCollege());
map.put("grade", MainActivity.personalInfo.getGrade());
map.put("major", MainActivity.personalInfo.getMajor());
map.put("department", MainActivity.personalInfo.getDepartment());
map.put("sex", MainActivity.personalInfo.getSex());
System.out.println("map:" + map);
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/clientuser/updateSave.do?token=" + token
+ "&singnature=" + singnature + "&st=" + st;
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW);
OkHttpUtils.getInstance().doPostAsync(ctx, http, map, handler);
}
}