Android手机相册和拍照实现

     具体类代码如下:

public class Register2Fragment extends Fragment {

	private Button mNextButton;
	private Button mBackButton;
	private LinearLayout mFooterLayout;
	private LinearLayout mBodyLayout;
	private Button photo;
	private Button camera;
	private ImageView image;
	private String local_path = Environment.getExternalStorageDirectory()+"/";//图片根路径
	private String path;//图片存储路径
//	private ProgressBar pb;  
//	private TextView upload;
	public static final int NONE = 0;  
	public static final int PHOTOHRAPH = 1;// 拍照  
	public static final int PHOTOZOOM = 2; // 缩放  
	public static final int PHOTORESOULT = 3;// 结果  
	public static final String IMAGE_UNSPECIFIED = "image/*";  
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_register2, parent, false);
		RegListener2 listener = new RegListener2();
		photo = (Button) v.findViewById(R.id.register_album_chosen);// 相册
		photo.setOnClickListener(listener);

		camera = (Button) v.findViewById(R.id.register_kacha);// 拍照
		camera.setOnClickListener(listener);

		image = (ImageView) v.findViewById(R.id.reigster2_image);// 显示照片

		mBackButton = (Button) v.findViewById(R.id.register1_3_back);// 返回
		mBackButton.setOnClickListener(listener);

		mNextButton = (Button) v.findViewById(R.id.register1_3_next);// 点击“下一步”按钮
		mNextButton.setOnClickListener(listener);
		
//		pb = (ProgressBar) v.findViewById(R.id.uploadbar);//进度条
//		upload = (TextView) v.findViewById(R.id.uploadtext);

		LinearLayout mNavBackButton = (LinearLayout) v.findViewById(R.id.register2_nav_back);
		mNavBackButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				RegisterActivity.simulateKey(KeyEvent.KEYCODE_BACK);
			}
		});

		TextView mSkipButton = (TextView) v.findViewById(R.id.register2_skip);

		mSkipButton.setOnClickListener(listener);
		
		mFooterLayout=(LinearLayout) v.findViewById(R.id.register2_footer);//更换布局
		
		mBodyLayout=(LinearLayout) v.findViewById(R.id.register2_body);
		
		return v;
	}

	@SuppressWarnings("deprecation")
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == NONE)
			return;
		// 拍照
		if (requestCode == PHOTOHRAPH) {
			// 设置文件保存路径这里放在跟目录下
			File picture = new File(local_path + getUserName() + "_temp.jpg");
			System.out.println("路径为:" + Uri.fromFile(picture));
			startPhotoZoom(Uri.fromFile(picture));
		}

		if (data == null)
			return;

		// 读取相册缩放图片
		if (requestCode == PHOTOZOOM) {
			startPhotoZoom(data.getData());
			System.out.println("data=" + data.getData());
			Uri originalUri = data.getData();// 得到图片的URI
			String[] imgs = { MediaStore.Images.Media.DATA };// 将图片URI转换成存储路径
			Cursor cursor = getActivity().managedQuery(originalUri, imgs, null, null, null);
			int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();
			path = cursor.getString(index);
			System.out.println("image save path = " + path);
		}
		// 处理结果
		if (requestCode == PHOTORESOULT) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 -
				System.out.println("photo="+photo);														// 100)压缩文件
				image.setImageBitmap(photo);
			}
			showFooter();
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void showFooter() {
		mFooterLayout.setVisibility(View.VISIBLE);
		LinearLayout.LayoutParams body_layout=(LayoutParams) mBodyLayout.getLayoutParams();
		body_layout.weight=10;
	}

	public void startPhotoZoom(Uri uri) {
		System.out.println("uri="+uri);
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1.5);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 140);
		intent.putExtra("outputY", 180);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTORESOULT);
		System.out.println("diaoyongle!!!!");
	}

	// 监听器
	private class RegListener2 implements OnClickListener {
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.register_album_chosen:
				Intent intent = new Intent(Intent.ACTION_PICK, null);
				intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						IMAGE_UNSPECIFIED);
				startActivityForResult(intent, PHOTOZOOM);
				break;
			case R.id.register_kacha:
				Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(local_path , getUserName() + "_temp.jpg")));
				startActivityForResult(intent1, PHOTOHRAPH);
				path = local_path + getUserName() + "_temp.jpg";
				System.out.println("path======||||||" + path);
				break;
			case R.id.register1_3_back:
				RegisterActivity.simulateKey(KeyEvent.KEYCODE_BACK);
				break;
			case R.id.register1_3_next:
				// 将获取的照片路径存入
				TagDNAApplication.getInstance().setAttribute("path", path);
				HttpTask.getInstance().execUpload(TagURL.PHOTO_URL, path, new CallBack()
				{
					@Override
					public void onTimeOut()
					{
						
					}
					
					@Override
					public void onSuccess(String result)
					{
						
					}
					
					@Override
					public void onProgress(int progress)
					{
						
					}
					
					@Override
					public void onError(Exception e)
					{
						e.printStackTrace();
					}
				});
				
			case R.id.register2_skip:
				// 将获取的照片路径存入
				TagDNAApplication.getInstance().setAttribute("path", path);
				
				FragmentManager manager = getActivity()
						.getSupportFragmentManager();

				manager.beginTransaction()
						.setCustomAnimations(R.animator.slide_in_right,
								R.animator.slide_out_left,
								R.animator.slide_in_left,
								R.animator.slide_out_right)
						.hide(manager.findFragmentByTag("register2"))
						.show(manager.findFragmentByTag("register3"))
						.addToBackStack("register2back").commit();
			}
		}
	}
	
	public String getUserName(){
		String  userName = TagDNAApplication.getInstance().getAttribute("nickName");
		return userName;
	}
}

 

你可能感兴趣的:(android)