1. 程式人生 > >相機相簿獲取照片加擷取

相機相簿獲取照片加擷取

package com.example.yuekao6.fragment;

import java.io.File;

import com.example.yuekao6.R;

import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView;

public class CFragment extends Fragment {

private ImageView pic;

// 設定相機拍攝圖片的名字
String path = Environment.getExternalStorageDirectory() + "/kk.jsp";

@Override
public View onCreateView(LayoutInflater inflater,
		@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	View view = View.inflate(getActivity(), R.layout.cfragment, null);
	pic = (ImageView) view.findViewById(R.id.pic);
	pic.setOnClickListener(new OnClickListener() {

		private AlertDialog create;

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AlertDialog.Builder builder = new AlertDialog.Builder(
					getActivity());
			create = builder.create();
			View view = View.inflate(getActivity(), R.layout.item_button,
					null);
			create.setView(view);
			create.show();
			Button xiangce = (Button) view.findViewById(R.id.xiangce);
			Button xiangji = (Button) view.findViewById(R.id.xiangji);
			xiangce.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					xiangce();
					create.dismiss();
				}

			});
			xiangji.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					xiangji();
					create.dismiss();
				}

			});
		}
	});
	return view;

}

private void xiangce() {
	// TODO Auto-generated method stub
	Intent intent = new Intent(Intent.ACTION_PICK);
	intent.setType("image/*");
	startActivityForResult(intent, 1000);
}

private void xiangji() {
	// TODO Auto-generated method stub
	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
	startActivityForResult(intent, 2000);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
	// TODO Auto-generated method stub
	super.onActivityResult(requestCode, resultCode, data);
	if (requestCode == 1000) {//相簿
		Uri uri = data.getData();
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 設定圖片的寬高
		intent.putExtra("aspactX", 1);
		intent.putExtra("aspactY", 1);
		intent.putExtra("outputX", 250);
		intent.putExtra("outputY", 250);

		intent.putExtra("return-data", true);
		startActivityForResult(intent, 3000);
	}
	if (requestCode == 2000) {//相機
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
		// 設定圖片的寬高
		intent.putExtra("aspactX", 1);
		intent.putExtra("aspactY", 1);
		intent.putExtra("outputX", 250);
		intent.putExtra("outputY", 250);
		
		intent.putExtra("return-data", true);
		startActivityForResult(intent, 3000);
	}
	if (requestCode == 3000) {
		Bitmap bitmap = data.getParcelableExtra("data");
		pic.setImageBitmap(bitmap);
	}
}

}