Java tutorial
/** * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ccxt.whl.activity; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; import org.apache.http.Header; import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v4.app.Fragment; import android.text.InputType; import android.text.TextUtils; import android.text.method.NumberKeyListener; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.ccxt.whl.Constant; import com.ccxt.whl.DemoApplication; import com.ccxt.whl.R; import com.ccxt.whl.gushi.User_gushi_Activity; import com.ccxt.whl.utils.CommonUtils; import com.ccxt.whl.utils.DeviceUuidFactory; import com.ccxt.whl.utils.HttpRestClient; import com.ccxt.whl.utils.ImageOptions; import com.ccxt.whl.utils.JsonToMapList; import com.ccxt.whl.utils.MyLogger; import com.ccxt.whl.utils.PreferenceUtils; import com.easemob.chat.EMChatManager; import com.easemob.chat.EMChatOptions; import com.easemob.util.PathUtil; import com.loopj.android.http.BaseJsonHttpResponseHandler; import com.loopj.android.http.RequestParams; import com.nostra13.universalimageloader.core.ImageLoader; /** * ? * * @author Administrator * */ public class SettingsFragment extends Fragment implements OnClickListener { /* private String = "shared_key_setting_user_pic"; private String = "shared_key_setting_user_sex"; private String = "shared_key_setting_user_age"; private String = "shared_key_setting_user_area"; private String = "shared_key_setting_user_zaina";*/ private static MyLogger Log = MyLogger.yLog(); /**********************************?*****************************************/ /** * ? */ private RelativeLayout rl_user_pic; /** * */ private RelativeLayout rl_user_nicheng; /** * */ private RelativeLayout rl_user_xingbie; /** * */ private RelativeLayout rl_user_nianling; /** * */ private RelativeLayout rl_user_chengshi; /** * ? */ private RelativeLayout rl_user_zhiye; /** * ?? */ private RelativeLayout rl_user_qianming; /** * ? */ private RelativeLayout rl_user_zainadongtai; /** * ?imageView */ private ImageView iv_user_photo; /** * */ private TextView tv_user_nicheng; /** * */ private TextView tv_user_xingbie; /** * */ private TextView tv_user_nianling; /** * */ private TextView tv_user_chengshi; /** * ? */ private TextView tv_user_zhiye; /** * ?? */ private TextView tv_user_qianming; /** * ? */ public TextView tv_user_zainadongtai; private String UserPic = null; private String UserNickName = null; private String UserSex = null; private String UserAge = null; private String UserArea = null; private String UserZaina = null; private String UserZhiye = null; private String UserQianming = null; /******************************************************************************/ /** * ? */ private RelativeLayout rl_switch_notification; /** * */ private RelativeLayout rl_switch_sound; /** * */ private RelativeLayout rl_switch_vibrate; /** * */ private RelativeLayout rl_switch_speaker; /** * ?imageView */ private ImageView iv_switch_open_notification; /** * ?imageview */ private ImageView iv_switch_close_notification; /** * ??imageview */ private ImageView iv_switch_open_sound; /** * ??imageview */ private ImageView iv_switch_close_sound; /** * ??? */ private ImageView iv_switch_open_vibrate; /** * ??? */ private ImageView iv_switch_close_vibrate; /** * */ private ImageView iv_switch_open_speaker; /** * */ private ImageView iv_switch_close_speaker; /** * ? */ private TextView textview1, textview2; private LinearLayout blacklistContainer; /** * */ private Button logoutBtn; private Button exit; private EMChatOptions chatOptions; /************* * ?? */ private BaseJsonHttpResponseHandler responseHandler; private File cameraFile;//? // private File cutFile;//? //?uri private static String IMAGE_FILE_LOCATION = null; private Uri imageUri = null; private static final String IMAGE_FILE_LOCATION_TEAST = "file:///sdcard/lehu/temp.jpg";//temp file Uri imageUritest = Uri.parse(IMAGE_FILE_LOCATION_TEAST);//The Uri to store the big bitmap //private Uri imageUri = null;//Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap public static final int USERPIC_REQUEST_CODE_LOCAL = 101; public static final int USERPIC_REQUEST_CODE_LOCAL_19 = 101; public static final int USERPIC_REQUEST_CODE_CAMERA = 102; public static final int USERPIC_REQUEST_CODE_CUT = 103; private ProgressDialog pd; //id private static String uid = null; /** * */ private LinearLayout llDiagnose; /** * ? */ private LinearLayout wo_gushi; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_conversation_settings, container, false); } /* rl_user_pic rl_user_nicheng rl_user_xingbie rl_user_nianling rl_user_chengshi rl_user_zainadongtai iv_user_photo tv_user_nicheng tv_user_xingbie tv_user_nianling tv_user_chengshi tv_user_zainadongtai UserPic UserNickName UserSex UserAge UserArea UserZaina */ @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); DeviceUuidFactory uuid = new DeviceUuidFactory(getActivity()); uid = uuid.getDeviceUuid().toString(); pd = new ProgressDialog(getActivity()); pd.setMessage("??..."); /***************?**************/ rl_user_pic = (RelativeLayout) getView().findViewById(R.id.rl_user_pic); rl_user_nicheng = (RelativeLayout) getView().findViewById(R.id.rl_user_nicheng); rl_user_xingbie = (RelativeLayout) getView().findViewById(R.id.rl_user_xingbie); rl_user_nianling = (RelativeLayout) getView().findViewById(R.id.rl_user_nianling); rl_user_chengshi = (RelativeLayout) getView().findViewById(R.id.rl_user_chengshi); rl_user_zainadongtai = (RelativeLayout) getView().findViewById(R.id.rl_user_zainadongtai); //--------- rl_user_zhiye = (RelativeLayout) getView().findViewById(R.id.rl_user_zhiye); rl_user_qianming = (RelativeLayout) getView().findViewById(R.id.rl_user_qianming); //? iv_user_photo = (ImageView) getView().findViewById(R.id.iv_user_photo); tv_user_nicheng = (TextView) getView().findViewById(R.id.tv_user_nicheng); tv_user_xingbie = (TextView) getView().findViewById(R.id.tv_user_xingbie); tv_user_nianling = (TextView) getView().findViewById(R.id.tv_user_nianling); tv_user_chengshi = (TextView) getView().findViewById(R.id.tv_user_chengshi); tv_user_zainadongtai = (TextView) getView().findViewById(R.id.tv_user_zainadongtai); //-------- tv_user_zhiye = (TextView) getView().findViewById(R.id.tv_user_zhiye); tv_user_qianming = (TextView) getView().findViewById(R.id.tv_user_qianming); //?? UserPic = PreferenceUtils.getInstance(getActivity()).getSettingUserPic(); UserNickName = PreferenceUtils.getInstance(getActivity()).getSettingUserNickName(); UserSex = PreferenceUtils.getInstance(getActivity()).getSettingUserSex(); UserAge = PreferenceUtils.getInstance(getActivity()).getSettingUserAge(); UserArea = PreferenceUtils.getInstance(getActivity()).getSettingUserArea(); UserZaina = PreferenceUtils.getInstance(getActivity()).getSettingUserZaina(); //----- UserZhiye = PreferenceUtils.getInstance(getActivity()).getSettingUserZhiye(); UserQianming = PreferenceUtils.getInstance(getActivity()).getSettingUserQianming(); //? //iv_user_photo ImageLoader.getInstance().displayImage(UserPic, iv_user_photo, ImageOptions.getOptions()); tv_user_nicheng.setText(UserNickName); tv_user_xingbie.setText(UserSex); tv_user_nianling.setText(UserAge); tv_user_chengshi.setText(UserArea); tv_user_zhiye.setText(UserZhiye); tv_user_qianming.setText(UserQianming); if (CommonUtils.isNullOrEmpty(UserZaina)) { UserZaina = "???"; } tv_user_zainadongtai.setText(UserZaina); /***************?**************/ rl_switch_notification = (RelativeLayout) getView().findViewById(R.id.rl_switch_notification); rl_switch_sound = (RelativeLayout) getView().findViewById(R.id.rl_switch_sound); rl_switch_vibrate = (RelativeLayout) getView().findViewById(R.id.rl_switch_vibrate); rl_switch_speaker = (RelativeLayout) getView().findViewById(R.id.rl_switch_speaker); iv_switch_open_notification = (ImageView) getView().findViewById(R.id.iv_switch_open_notification); iv_switch_close_notification = (ImageView) getView().findViewById(R.id.iv_switch_close_notification); iv_switch_open_sound = (ImageView) getView().findViewById(R.id.iv_switch_open_sound); iv_switch_close_sound = (ImageView) getView().findViewById(R.id.iv_switch_close_sound); iv_switch_open_vibrate = (ImageView) getView().findViewById(R.id.iv_switch_open_vibrate); iv_switch_close_vibrate = (ImageView) getView().findViewById(R.id.iv_switch_close_vibrate); iv_switch_open_speaker = (ImageView) getView().findViewById(R.id.iv_switch_open_speaker); iv_switch_close_speaker = (ImageView) getView().findViewById(R.id.iv_switch_close_speaker); llDiagnose = (LinearLayout) getView().findViewById(R.id.ll_diagnose); logoutBtn = (Button) getView().findViewById(R.id.btn_logout); exit = (Button) getView().findViewById(R.id.btn_exit); if (!TextUtils.isEmpty(EMChatManager.getInstance().getCurrentUser())) { //logoutBtn.setText(getString(R.string.button_logout) + "(" + EMChatManager.getInstance().getCurrentUser() + ")"); logoutBtn.setText(getString(R.string.button_logout)); } textview1 = (TextView) getView().findViewById(R.id.textview1); textview2 = (TextView) getView().findViewById(R.id.textview2); blacklistContainer = (LinearLayout) getView().findViewById(R.id.ll_black_list); wo_gushi = (LinearLayout) getView().findViewById(R.id.wo_gushi); /*********************/ rl_user_pic.setOnClickListener(this); rl_user_nicheng.setOnClickListener(this); rl_user_xingbie.setOnClickListener(this); rl_user_nianling.setOnClickListener(this); rl_user_chengshi.setOnClickListener(this); rl_user_zainadongtai.setOnClickListener(this); rl_user_zhiye.setOnClickListener(this); rl_user_qianming.setOnClickListener(this); /*********************/ blacklistContainer.setOnClickListener(this); rl_switch_notification.setOnClickListener(this); rl_switch_sound.setOnClickListener(this); rl_switch_vibrate.setOnClickListener(this); rl_switch_speaker.setOnClickListener(this); llDiagnose.setOnClickListener(this); logoutBtn.setOnClickListener(this); exit.setOnClickListener(this); wo_gushi.setOnClickListener(this); chatOptions = EMChatManager.getInstance().getChatOptions(); if (chatOptions.getNotificationEnable()) { iv_switch_open_notification.setVisibility(View.VISIBLE); iv_switch_close_notification.setVisibility(View.INVISIBLE); } else { iv_switch_open_notification.setVisibility(View.INVISIBLE); iv_switch_close_notification.setVisibility(View.VISIBLE); } if (chatOptions.getNoticedBySound()) { iv_switch_open_sound.setVisibility(View.VISIBLE); iv_switch_close_sound.setVisibility(View.INVISIBLE); } else { iv_switch_open_sound.setVisibility(View.INVISIBLE); iv_switch_close_sound.setVisibility(View.VISIBLE); } if (chatOptions.getNoticedByVibrate()) { iv_switch_open_vibrate.setVisibility(View.VISIBLE); iv_switch_close_vibrate.setVisibility(View.INVISIBLE); } else { iv_switch_open_vibrate.setVisibility(View.INVISIBLE); iv_switch_close_vibrate.setVisibility(View.VISIBLE); } if (chatOptions.getUseSpeaker()) { iv_switch_open_speaker.setVisibility(View.VISIBLE); iv_switch_close_speaker.setVisibility(View.INVISIBLE); } else { iv_switch_open_speaker.setVisibility(View.INVISIBLE); iv_switch_close_speaker.setVisibility(View.VISIBLE); } /*************************http?***********************/ responseHandler = new BaseJsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Object response) { // TODO Auto-generated method stub Log.d("setting_qes" + rawJsonResponse); pd.dismiss(); if (CommonUtils.isNullOrEmpty(rawJsonResponse)) { Toast.makeText(getActivity(), "?,?", 0).show(); return; } Map<String, Object> lm = JsonToMapList.getMap(rawJsonResponse); if (lm.get("status").toString() != null && lm.get("status").toString().equals("yes")) { Toast.makeText(getActivity(), "?", 0).show(); Log.d("message==" + lm.get("message").toString()); if (!CommonUtils.isNullOrEmpty(lm.get("result").toString())) { Map<String, Object> lmres = JsonToMapList.getMap(lm.get("result").toString()); String nickname = lmres.get("nickname").toString(); String age = lmres.get("age").toString(); String sex = lmres.get("sex").toString(); String headurl = lmres.get("headurl").toString(); String zhiye = lmres.get("zhiye").toString(); String qianming = lmres.get("qianming").toString(); if (!CommonUtils.isNullOrEmpty(nickname)) { tv_user_nicheng.setText(nickname); PreferenceUtils.getInstance(getActivity()).setSettingUserNickName(nickname); } else if (!CommonUtils.isNullOrEmpty(age)) { tv_user_nianling.setText(age); PreferenceUtils.getInstance(getActivity()).setSettingUserAge(age); } else if (!CommonUtils.isNullOrEmpty(sex)) { tv_user_xingbie.setText(sex); PreferenceUtils.getInstance(getActivity()).setSettingUserSex(age); } else if (!CommonUtils.isNullOrEmpty(headurl)) { //tv_user_xingbie.setText(sex); //? ImageLoader.getInstance().displayImage(headurl, iv_user_photo, ImageOptions.getOptions()); //ImageLoader.getInstance().displayImage(headurl, iv_user_photo); //ImageLoader.getInstance().notify(); PreferenceUtils.getInstance(getActivity()).setSettingUserPic(headurl); } else if (!CommonUtils.isNullOrEmpty(zhiye)) {//? tv_user_zhiye.setText(zhiye); PreferenceUtils.getInstance(getActivity()).setSettingUserZhiye(zhiye); } else if (!CommonUtils.isNullOrEmpty(qianming)) {//?? tv_user_qianming.setText(qianming); PreferenceUtils.getInstance(getActivity()).setSettingUserQianming(qianming); } } } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, Object errorResponse) { // TODO Auto-generated method stub pd.dismiss(); Toast.makeText(getActivity(), ",?", 0).show(); return; } @Override protected Object parseResponse(String rawJsonData, boolean isFailure) throws Throwable { // TODO Auto-generated method stub return null; } }; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.rl_user_pic: //startActivity(new Intent(getActivity(), BlacklistActivity.class)); //? change_headurl(); break; case R.id.rl_user_nicheng: //startActivity(new Intent(getActivity(), BlacklistActivity.class)); // change_nickname("" + tv_user_nicheng.getText().toString().trim()); break; case R.id.rl_user_xingbie: // change_sex(); break; case R.id.rl_user_nianling: // change_age("" + tv_user_nianling.getText().toString().trim()); case R.id.rl_user_chengshi: // break; case R.id.rl_user_zhiye: //? change_zhiye(); break; case R.id.rl_user_qianming: //?? change_qianming("" + tv_user_qianming.getText().toString().trim()); break; case R.id.rl_user_zainadongtai: //? break; case R.id.rl_switch_notification: if (iv_switch_open_notification.getVisibility() == View.VISIBLE) { iv_switch_open_notification.setVisibility(View.INVISIBLE); iv_switch_close_notification.setVisibility(View.VISIBLE); rl_switch_sound.setVisibility(View.GONE); rl_switch_vibrate.setVisibility(View.GONE); textview1.setVisibility(View.GONE); textview2.setVisibility(View.GONE); chatOptions.setNotificationEnable(false); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgNotification(false); } else { iv_switch_open_notification.setVisibility(View.VISIBLE); iv_switch_close_notification.setVisibility(View.INVISIBLE); rl_switch_sound.setVisibility(View.VISIBLE); rl_switch_vibrate.setVisibility(View.VISIBLE); textview1.setVisibility(View.VISIBLE); textview2.setVisibility(View.VISIBLE); chatOptions.setNotificationEnable(true); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgNotification(true); } break; case R.id.rl_switch_sound: if (iv_switch_open_sound.getVisibility() == View.VISIBLE) { iv_switch_open_sound.setVisibility(View.INVISIBLE); iv_switch_close_sound.setVisibility(View.VISIBLE); chatOptions.setNoticeBySound(false); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgSound(false); } else { iv_switch_open_sound.setVisibility(View.VISIBLE); iv_switch_close_sound.setVisibility(View.INVISIBLE); chatOptions.setNoticeBySound(true); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgSound(true); } break; case R.id.rl_switch_vibrate: if (iv_switch_open_vibrate.getVisibility() == View.VISIBLE) { iv_switch_open_vibrate.setVisibility(View.INVISIBLE); iv_switch_close_vibrate.setVisibility(View.VISIBLE); chatOptions.setNoticedByVibrate(false); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgVibrate(false); } else { iv_switch_open_vibrate.setVisibility(View.VISIBLE); iv_switch_close_vibrate.setVisibility(View.INVISIBLE); chatOptions.setNoticedByVibrate(true); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgVibrate(true); } break; case R.id.rl_switch_speaker: if (iv_switch_open_speaker.getVisibility() == View.VISIBLE) { iv_switch_open_speaker.setVisibility(View.INVISIBLE); iv_switch_close_speaker.setVisibility(View.VISIBLE); chatOptions.setUseSpeaker(false); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgSpeaker(false); } else { iv_switch_open_speaker.setVisibility(View.VISIBLE); iv_switch_close_speaker.setVisibility(View.INVISIBLE); chatOptions.setUseSpeaker(true); EMChatManager.getInstance().setChatOptions(chatOptions); PreferenceUtils.getInstance(getActivity()).setSettingMsgVibrate(true); } break; case R.id.btn_logout: DemoApplication.getInstance().logout(); // ?? ((MainActivity) getActivity()).finish(); startActivity(new Intent(getActivity(), LoginActivity.class)); break; case R.id.btn_exit: //DemoApplication.getInstance().logout(); // ?? //((MainActivity) getActivity()).finish(); android.os.Process.killProcess(android.os.Process.myPid()); //?PID System.exit(0); //java?c#0 break; case R.id.ll_black_list: startActivity(new Intent(getActivity(), BlacklistActivity.class)); break; case R.id.ll_diagnose: startActivity(new Intent(getActivity(), DiagnoseActivity.class)); break; case R.id.wo_gushi: startActivity(new Intent(getActivity(), User_gushi_Activity.class).putExtra("userId", DemoApplication.getInstance().getUser())); break; default: break; } } /** * ? */ private void change_zhiye() { // TODO Auto-generated method stub AlertDialog.Builder builder = new Builder(getActivity()); String[] strarr = { "/?/", "//", "/?/??", "?///?", "//", "?//", "/?/?", "/", "/", "/??", "", "" }; builder.setItems(strarr, new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { String zhiye = ""; switch (arg1) { case 0: zhiye = "/?/"; break; case 1: zhiye = "//"; break; case 2: zhiye = "/?/??"; break; case 3: zhiye = "?///?"; break; case 4: zhiye = "//"; break; case 5: zhiye = "?//"; break; case 6: zhiye = "/?/?"; break; case 7: zhiye = "/"; break; case 8: zhiye = "/"; break; case 9: zhiye = "/??"; break; case 10: zhiye = ""; break; default: zhiye = ""; break; } RequestParams params = new RequestParams(); params.add("user", DemoApplication.getInstance().getUser()); params.add("zhiye", zhiye); params.add("param", "zhiye"); params.add("uid", uid); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); pd.show(); } }); builder.show(); } /** * ?? */ private void change_qianming(String qianming) { // TODO Auto-generated method stub final EditText texta = new EditText(getActivity()); texta.setText(qianming); new AlertDialog.Builder(getActivity()).setTitle("??") .setIcon(android.R.drawable.ic_dialog_info).setView(texta) .setPositiveButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String nickname = texta.getEditableText().toString(); RequestParams params = new RequestParams(); params.add("user", DemoApplication.getInstance().getUser()); params.add("qianming", nickname); params.add("param", "qianming"); params.add("uid", uid); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); pd.show(); dialog.dismiss(); //? } }).setNegativeButton("?", null).show(); } /** * */ public void change_age(String age) { final EditText texta = new EditText(getActivity()); texta.setText(age); //EditText texta.setKeyListener(new NumberKeyListener() { public int getInputType() { return InputType.TYPE_CLASS_PHONE; } @Override protected char[] getAcceptedChars() { // TODO Auto-generated method stub char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; return numbers; } }); new AlertDialog.Builder(getActivity()).setTitle("") .setIcon(android.R.drawable.ic_dialog_info).setView(texta) .setPositiveButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String age = texta.getEditableText().toString(); RequestParams params = new RequestParams(); params.add("user", DemoApplication.getInstance().getUser()); params.add("age", age); params.add("param", "age"); params.add("uid", uid); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); pd.show(); dialog.dismiss(); //? } }).setNegativeButton("?", null).show(); //return true; } /** * */ public void change_nickname(String nickname) { final EditText texta = new EditText(getActivity()); texta.setText(nickname); new AlertDialog.Builder(getActivity()).setTitle("") .setIcon(android.R.drawable.ic_dialog_info).setView(texta) .setPositiveButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String nickname = texta.getEditableText().toString(); RequestParams params = new RequestParams(); params.add("user", DemoApplication.getInstance().getUser()); params.add("nickname", nickname); params.add("param", "nickname"); params.add("uid", uid); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); pd.show(); dialog.dismiss(); //? } }).setNegativeButton("?", null).show(); //return true; } /** * */ public void change_sex() { AlertDialog.Builder builder = new Builder(getActivity()); String[] strarr = { "", "" }; builder.setItems(strarr, new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { String sex = "2"; // TODO ? if (arg1 == 0) {// sex = "1"; } else {// sex = "2"; } RequestParams params = new RequestParams(); params.add("user", DemoApplication.getInstance().getUser()); params.add("sex", sex); params.add("param", "sex"); params.add("uid", uid); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); pd.show(); } }); builder.show(); } /** * ? */ public void change_headurl() { /*//SDcard if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //SDcardTestSyncListView IMAGE_FILE_LOCATION = Environment.getExternalStorageDirectory()+Constant.CACHE_DIR_IMAGE+"/temp.jpg"; imageUri = Uri.parse(IMAGE_FILE_LOCATION); //File cutFile = new File(Constant.CACHE_DIR_IMAGE,"temp.jpg"); //cutFile.getParentFile().mkdirs(); File dir = new File(IMAGE_FILE_LOCATION); if (!dir.exists()) { try { // dir.createNewFile(); } catch (Exception e) { //println(e); } } }else{ Toast.makeText(getActivity(), "SD????", 0).show(); return; }*/ /*-?*/ if (!CommonUtils.isExitsSdcard()) { Toast.makeText(getActivity(), "SD????", 0).show(); return; } cameraFile = new File(PathUtil.getInstance().getImagePath(), DemoApplication.getInstance().getUser() + System.currentTimeMillis() + ".jpg"); if (cameraFile == null && !cameraFile.exists()) {//? cameraFile.getParentFile().mkdirs(); } AlertDialog.Builder builder = new Builder(getActivity()); String[] strarr = { "?", "" }; builder.setItems(strarr, new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO ? if (arg1 == 0) {//? selectPicFromCamera(); } else {// selectPicFromLocal(); } } }); builder.show(); } /** * ? */ public void selectPicFromLocal() { Intent intent; if (Build.VERSION.SDK_INT < 19) { intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); //Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); /*intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 600); intent.putExtra("outputY", 600); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUritest); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection */ //startActivityForResult(intent, CHOOSE_BIG_PICTURE); } else { intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //startActivityForResult(intent, USERPIC_REQUEST_CODE_LOCAL_19); } startActivityForResult(intent, USERPIC_REQUEST_CODE_LOCAL); } /** * ? */ public void selectPicFromCamera() { // cameraFile = new File(PathUtil.getInstance().getImagePath(), DemoApplication.getInstance().getUserName() startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile)), USERPIC_REQUEST_CODE_CAMERA); } /** * onActivityResult */ public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == USERPIC_REQUEST_CODE_CAMERA) { // ? if (cameraFile != null && cameraFile.exists()) { Log.d("cameraFile" + cameraFile.getAbsolutePath()); //?uri imageUri = Uri.fromFile(cameraFile); cropImageUri(Uri.fromFile(cameraFile), 300, 300, USERPIC_REQUEST_CODE_CUT); } } else if (requestCode == USERPIC_REQUEST_CODE_LOCAL) { // ? if (data != null) { Uri selectedImage = data.getData(); if (selectedImage != null) { cropImageUri(selectedImage, 300, 300, USERPIC_REQUEST_CODE_CUT); //Log.d("log","selectedImage"+selectedImage); } } } else if (requestCode == USERPIC_REQUEST_CODE_CUT) {//? // ? if (data != null) { Bitmap bitmap = data.getParcelableExtra("data"); iv_user_photo.setImageBitmap(bitmap); File file = saveJPGE_After(bitmap, cameraFile); //???? RequestParams params = new RequestParams(); if (file.exists()) { try { params.put("headurl", file, "image/jpeg"); params.put("user", DemoApplication.getInstance().getUser()); params.put("param", "headurl"); params.add("uid", uid); HttpRestClient.post(Constant.UPDATE_USER_URL, params, responseHandler); pd.show(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Toast toast = Toast.makeText(getActivity(), "?SD??", Toast.LENGTH_SHORT); } } else { // Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data); } // Log.d("log","imageUribundle==>"+imageUri); //iv_user_photo.setImageURI(imageUri); //params.put(key, file, ) //**??data //Bitmap bitmap = data.getParcelableExtra("data"); // Bitmap bitmap = data.getExtras().getParcelable("data"); /* // ByteArrayOutputStream out = new ByteArrayOutputStream(); // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); */ //this.iv_image.setImageBitmap(bitmap); /* }else{ Toast toast = Toast.makeText(getActivity(), "?SD??", Toast.LENGTH_SHORT); } */ /* try { // // tempFile.delete(); } catch (Exception e) { e.printStackTrace(); } */ /* RequestParams params = new RequestParams(); final String contentType = RequestParams.APPLICATION_OCTET_STREAM;*/ // params.put(key, file, contentType) //HttpRestClient.post(url, params, responseHandler) } } //uri bitmap private Bitmap decodeUriAsBitmap(Uri uri) { Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } return bitmap; } /** * ?uri?? * * @param selectedImage */ private File Uritofile(Uri selectedImage) { File file = null; Cursor cursor = getActivity().getContentResolver().query(selectedImage, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex("_data"); String picturePath = cursor.getString(columnIndex); cursor.close(); cursor = null; if (picturePath == null || picturePath.equals("null")) { Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return null; } file = new File(picturePath); //sendPicture(picturePath); } else { file = new File(selectedImage.getPath()); if (!file.exists()) { Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return null; } } return file; } /** * ?uri? * * @param selectedImage */ private void sendPicByUri(Uri selectedImage) { // String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().getContentResolver().query(selectedImage, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex("_data"); String picturePath = cursor.getString(columnIndex); cursor.close(); cursor = null; if (picturePath == null || picturePath.equals("null")) { Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } //copyFile(picturePath,imageUri.getPath()); cropImageUri(selectedImage, 200, 200, USERPIC_REQUEST_CODE_CUT); //sendPicture(picturePath); } else { File file = new File(selectedImage.getPath()); if (!file.exists()) { Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } //copyFile(selectedImage.getPath(),imageUri.getPath()); cropImageUri(selectedImage, 200, 200, USERPIC_REQUEST_CODE_CUT); //sendPicture(file.getAbsolutePath()); } } /** * ?uri? * * @param selectedImage */ private String getpathfromUri(Uri selectedImage) { // String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().getContentResolver().query(selectedImage, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex("_data"); String picturePath = cursor.getString(columnIndex); cursor.close(); cursor = null; if (picturePath == null || picturePath.equals("null")) { Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return null; } return picturePath; //sendPicture(picturePath); } else { File file = new File(selectedImage.getPath()); if (!file.exists()) { Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return null; } return file.getAbsolutePath(); } } /** * ? * @param uri * @param outputX * @param outputY * @param requestCode */ private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) { /*Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); //aspectX aspectY intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY ? intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode);*/ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); } /** * ?Bitmap * @param baseBitmap */ public void save(Bitmap baseBitmap) { try { /* BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(cutFile)); baseBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close();*/ OutputStream stream = new FileOutputStream(Uritofile(imageUri)); baseBitmap.compress(CompressFormat.JPEG, 100, stream); stream.close(); /* // sdcard Intent intent = new Intent(); intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment .getExternalStorageDirectory())); sendBroadcast(intent);*/ Toast.makeText(getActivity(), "??", 0).show(); } catch (Exception e) { Toast.makeText(getActivity(), "?", 0).show(); e.printStackTrace(); } } /** * bitmap file * ?JPEG * @param bitmap * @param cameraFile2 */ public File saveJPGE_After(Bitmap bitmap, File cameraFile2) { //File file = new File(cameraFile2); try { FileOutputStream out = new FileOutputStream(cameraFile2); if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) { out.flush(); out.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return cameraFile2; } /** * ?? * @param oldPath String c:/fqf.txt * @param newPath String ?? f:/fqf.txt * @return boolean */ public void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { // InputStream inStream = new FileInputStream(oldPath); // FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; // ? //Log.d("log",bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { Log.d("???"); e.printStackTrace(); } } }