package com.geo.smallcredit.activity; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import com.geo.smallcredit.R; import com.geo.smallcredit.util.BitmapUtil; import com.geo.smallcredit.util.ToastUtil; import com.geo.smallcredit.vo.Popuw; public class SuggestionActivity extends Activity implements OnClickListener { private Button btn, imgback; private EditText edit_text; private TextView text_view; private int BigIndex = 100; private ImageView img, imageview; private View popupView; private PopupWindow popupWindow; LayoutInflater inflater; private LinearLayout mLine; // �Զ���ĵ������� Popuw menuWindow; private String temppath; private Uri tempuri; private File finalfile; private File tempFile; /* ������ʶ�������๦�� */ private static final int CAMERA_WITH_DATA = 50; /* ������ʶ����gallery */ private static final int PHOTO_PICKED_WITH_DATA = 60; /* ������ʶ�ü��ķ��� */ private static final int CUT_PHOTO = 70; private int createnum = 0; ImageView tv; private static final File PHOTO_DIR = new File( Environment.getExternalStorageDirectory() + "/dotOrderImage"); private TextView backtext; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.suggestion_activity); initview(); intent=getIntent(); backtext.setText(intent.getStringExtra("backText")); initClick(); // ����ⲿ������ʧ mLine = (LinearLayout) findViewById(R.id.traceroute_rootview); mLine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } }); } public void initview() { imgback = (Button) findViewById(R.id.suggestion_imgback); btn = (Button) findViewById(R.id.suggestion_btn); edit_text = (EditText) findViewById(R.id.suggestion_ed1); text_view = (TextView) findViewById(R.id.suggestion_tv1); img = (ImageView) findViewById(R.id.suggestion_img); imageview = (ImageView) findViewById(R.id.suggestion_imageview); backtext=(TextView) findViewById(R.id.suggestion_backtext); } public void initClick() { imgback.setOnClickListener(this); btn.setOnClickListener(this); edit_text.addTextChangedListener(new EditTextWatcher()); img.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.suggestion_imgback: this.finish(); break; case R.id.suggestion_btn: ToastUtil.show(SuggestionActivity.this, "��Ҫ��̨�ӿ�"); break; case R.id.suggestion_img: hintKbTwo(); // ʵ����SelectPicPopupWindow menuWindow = new Popuw(SuggestionActivity.this, itemsOnClick); // ��ʾ���� menuWindow.showAtLocation( SuggestionActivity.this.findViewById(R.id.suggestion_id), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // ����layout��PopupWindow����ʾ��λ�� break; } } public class EditTextWatcher implements TextWatcher { @Override public void afterTextChanged(Editable arg0) { String edit = edit_text.getText().toString(); edit_text.setVisibility(View.VISIBLE); if (edit.length() <= BigIndex) { text_view.setText("��������" + (BigIndex - edit.length()) + "��"); } else { edit_text.setText(edit.substring(0, BigIndex)); edit_text.setSelection(edit.substring(0, BigIndex).length()); } } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } } // ʹ��ϵͳ��ǰ���ڼ��Ե�����Ϊ��Ƭ������ private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; } @Override protected void onResume() { System.out.println("onResume"); // // �л���Ļ����ᵼ��activity�Ĵݻٺ��ؽ� // if (getRequestedOrientation() == // ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) { // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // System.out.println("��Ļ�л�"); // } super.onResume(); } /** * ����ü�֮���ͼƬ����   * * @param picdata */ private void setPicToView(Intent picdata) { Bundle extras = picdata.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); finalfile = new File(PHOTO_DIR, getPhotoFileName()); BitmapUtil.saveImg(photo, finalfile); // Log.i("finalfile", finalfile.getAbsolutePath()); Drawable drawable = new BitmapDrawable(photo); /** * ����ע�͵ķ����ǽ��ü�֮���ͼƬ��Base64Coder���ַ���ʽ�� ������������QQͷ���ϴ����õķ������������ */ /* * ByteArrayOutputStream stream = new ByteArrayOutputStream(); * photo.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] b * = stream.toByteArray(); // ��ͼƬ�����ַ�����ʽ�洢����  tp = new * String(Base64Coder.encodeLines(b)); * ����ط���ҿ���д�¸��������ϴ�ͼƬ��ʵ�֣�ֱ�Ӱ�tpֱ���ϴ��Ϳ����ˣ� ����������ķ����Ƿ������DZߵ����ˣ����  * ������ص��ķ����������ݻ�����Base64Coder����ʽ�Ļ������������·�ʽת�� Ϊ���ǿ����õ�ͼƬ���;�OK��...��� * Bitmap dBitmap = BitmapFactory.decodeFile(tp); Drawable drawable * = new BitmapDrawable(dBitmap); */ imageview.setImageDrawable(drawable); } } // Ϊ��������ʵ�ּ����� private OnClickListener itemsOnClick = new OnClickListener() { public void onClick(View v) { menuWindow.dismiss(); switch (v.getId()) { case R.id.btn_take_photo: { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // �������ָ������������պ����Ƭ�洢��·�� if (!PHOTO_DIR.exists()) { boolean iscreat = PHOTO_DIR.mkdirs();// ������Ƭ�Ĵ洢Ŀ¼ } tempFile = new File(PHOTO_DIR, getPhotoFileName()); // if (!tempFile.exists()) { // try { // tempFile.createNewFile(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } temppath = tempFile.getAbsolutePath(); tempuri = Uri.fromFile(tempFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, tempuri); startActivityForResult(intent, CAMERA_WITH_DATA); } break; case R.id.btn_pick_photo: { Intent intent = new Intent(Intent.ACTION_PICK, null); /** * ������仰����������ʽд��һ����Ч��������� intent.setData(MediaStore.Images * .Media.EXTERNAL_CONTENT_URI); * intent.setType(""image/*");������������ ���������Ҫ�����ϴ�����������ͼƬ����ʱ����ֱ��д�� * ��"image/jpeg �� image/png�ȵ�����" */ intent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, PHOTO_PICKED_WITH_DATA); } break; default: break; } } }; /** * �ü�ͼƬ����ʵ��   * * @param uri */ public void startPhotoZoom(Uri uri) { /* * �����������Intent��ACTION����ô֪���ģ���ҿ��Կ����Լ�·���µ�������ҳ * yourself_sdk_path/docs/reference/android/content/Intent.html * ֱ��������Ctrl+F�ѣ�CROP ��֮ǰû��ϸ��������ʵ��׿ϵͳ���Ѿ����Դ�ͼƬ�ü�����, ��ֱ�ӵ����ؿ�� */ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // �������crop=true�������ڿ�����Intent��������ʾ��VIEW�ɲü� intent.putExtra("crop", "true"); // aspectX aspectY �ǿ�ߵı��� intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY �Dzü�ͼƬ��� intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("return-data", true); startActivityForResult(intent, CUT_PHOTO); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { System.out.println("onActivityResult"); switch (requestCode) { // �����ֱ�Ӵ�����ȡ case PHOTO_PICKED_WITH_DATA: if (data != null && data.getData() != null) { startPhotoZoom(data.getData()); // System.out.println("data.getData()"+data.getData()); } break; // ����ǵ����������ʱ case CAMERA_WITH_DATA: Log.i("resultCode", resultCode + ""); if (resultCode == 0) { return; } Log.i("createnum", createnum + ""); startPhotoZoom(tempuri); break; // ȡ�òü����ͼƬ case CUT_PHOTO: /** * �ǿ��жϴ��һ��Ҫ��֤���������֤�Ļ��� �ڼ���֮��������ֲ����⣬Ҫ���²ü������� * ��ǰ����ʱ���ᱨNullException��ֻ ������ط����£���ҿ��Ը��ݲ�ͬ����ں��ʵ� �ط����жϴ����������   */ Log.i("CUT_PHOTO", resultCode + ""); // if(resultCode==0){ // return; // } if (data != null) { setPicToView(data); } break; default: break; } super.onActivityResult(requestCode, resultCode, data); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub System.out.println("onSaveInstanceState"); outState.putString("temppath", temppath); outState.putSerializable("finalfile", finalfile); super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // TODO Auto-generated method stub System.out.println("onRestoreInstanceState"); temppath = savedInstanceState.getString("temppath"); finalfile = (File) savedInstanceState.getSerializable("finalfile"); super.onRestoreInstanceState(savedInstanceState); } // �˷���ֻ�ǹر������ public void hintKbTwo() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive() && getCurrentFocus() != null) { if (getCurrentFocus().getWindowToken() != null) { imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } } }