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.InputStream; import java.io.OutputStream; import java.util.Map; import org.apache.http.Header; import android.app.AlertDialog; 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.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.util.Log; 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.utils.CommonUtils; import com.ccxt.whl.utils.HttpRestClient; import com.ccxt.whl.utils.ImageOptions; import com.ccxt.whl.utils.JsonToMapList; 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 SettingsFragmentCopy 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 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_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_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 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 EMChatOptions chatOptions; /************* * ?? */ private BaseJsonHttpResponseHandler responseHandler; private File cameraFile;//? // private File cutFile;//? //?uri private static String IMAGE_FILE_LOCATION = null; private Uri imageUri = null; //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_CAMERA = 102; public static final int USERPIC_REQUEST_CODE_CUT = 103; @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); /***************?**************/ 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); //? 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); //?? 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(); //? //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_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); logoutBtn = (Button) getView().findViewById(R.id.btn_logout); if (!TextUtils.isEmpty(EMChatManager.getInstance().getCurrentUser())) { logoutBtn.setText( getString(R.string.button_logout) + "(" + EMChatManager.getInstance().getCurrentUser() + ")"); } textview1 = (TextView) getView().findViewById(R.id.textview1); textview2 = (TextView) getView().findViewById(R.id.textview2); blacklistContainer = (LinearLayout) getView().findViewById(R.id.ll_black_list); /*********************/ 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); /*********************/ blacklistContainer.setOnClickListener(this); rl_switch_notification.setOnClickListener(this); rl_switch_sound.setOnClickListener(this); rl_switch_vibrate.setOnClickListener(this); rl_switch_speaker.setOnClickListener(this); logoutBtn.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("log", rawJsonResponse); 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("log", "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(); if (!CommonUtils.isNullOrEmpty(nickname)) { tv_user_nicheng.setText(nickname); } else if (!CommonUtils.isNullOrEmpty(age)) { tv_user_nianling.setText(age); } else if (!CommonUtils.isNullOrEmpty(sex)) { tv_user_xingbie.setText(sex); } else if (!CommonUtils.isNullOrEmpty(headurl)) { //tv_user_xingbie.setText(sex); //? } } } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, Object errorResponse) { // TODO Auto-generated method stub 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_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.ll_black_list: startActivity(new Intent(getActivity(), BlacklistActivity.class)); break; default: break; } } /** * */ 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"); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); 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"); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); 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"); HttpRestClient.get(Constant.UPDATE_USER_URL, params, responseHandler); } }); 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; } 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/*"); } else { intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); } /*//??? cutFile = new File(PathUtil.getInstance().getImagePath(), DemoApplication.getInstance().getUser() + System.currentTimeMillis() + "_m.jpg"); cutFile.getParentFile().mkdirs();*/ startActivityForResult(intent, USERPIC_REQUEST_CODE_LOCAL); } /** * ? */ public void selectPicFromCamera() { if (!CommonUtils.isExitsSdcard()) { Toast.makeText(getActivity(), "SD????", 0).show(); return; } // cameraFile = new File(PathUtil.getInstance().getImagePath(), DemoApplication.getInstance().getUserName() cameraFile = new File(PathUtil.getInstance().getImagePath(), DemoApplication.getInstance().getUser() + System.currentTimeMillis() + ".jpg"); cameraFile.getParentFile().mkdirs(); 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()) { //sendPicture(cameraFile.getAbsolutePath()); Log.d("log", "cameraFile" + cameraFile.getAbsolutePath()); //?uri imageUri = Uri.fromFile(cameraFile); imageUri = Uri.fromFile(cameraFile); cropImageUri(imageUri, 200, 200, USERPIC_REQUEST_CODE_CUT); } } else if (requestCode == USERPIC_REQUEST_CODE_LOCAL) { // ?? if (data != null) { Uri selectedImage = data.getData(); if (selectedImage != null) { //?uri imageUri = selectedImage; sendPicByUri(selectedImage); //Log.d("log","selectedImage"+cameraFile.getAbsolutePath()); } } } else if (requestCode == USERPIC_REQUEST_CODE_CUT) {//? // ? if (data != null) { Log.d("log", "imageUribundle==>" + imageUri); iv_user_photo.setImageURI(imageUri); RequestParams params = new RequestParams(); final String contentType = RequestParams.APPLICATION_OCTET_STREAM; File file = Uritofile(imageUri); //File file = new File(imageUri.getPath()); if (file.exists()) { try { params.put("headurl", file, "image/jpeg"); params.put("user", DemoApplication.getInstance().getUser()); params.put("param", "headurl"); HttpRestClient.post(Constant.UPDATE_USER_URL, params, responseHandler); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Toast toast = Toast.makeText(getActivity(), "?SD??", Toast.LENGTH_SHORT); } //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? * * @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(imageUri, 150, 150, 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(imageUri, 150, 150, USERPIC_REQUEST_CODE_CUT); //sendPicture(file.getAbsolutePath()); } /*if(getpathfromUri(uri)!=null&&getpathfromUri(imageUri)!=null){ copyFile(getpathfromUri(uri),getpathfromUri(imageUri)); }else{ return; }*/ //cropImageUri(selectedImage, 150, 150, USERPIC_REQUEST_CODE_CUT); } /** * ?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) { /*if(getpathfromUri(uri)!=null&&getpathfromUri(imageUri)!=null){ copyFile(getpathfromUri(uri),getpathfromUri(imageUri)); }else{ return; }*/ Log.d("log", uri.toString()); 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); } /** * ?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(); } } /** * ?? * @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("log", "???"); e.printStackTrace(); } } }