Android 调用系统的拍照,浏览图片,转存并裁剪

Android 调用系统的照相,浏览图片,转存并裁剪!public class AddCardActivity extends Activity {private ?

Android 调用系统的照相,浏览图片,转存并裁剪!

public class AddCardActivity extends Activity {


private ?Button add;

private ?Button take_p;

private Button app_ol;

private File sdCard;

private File sdPhoto;

?? private AlphaAnimation anim_alpha;?

?? ? private int menu = -1;

?? ? private final int LOCAL = 0;

?? ? private final int TAKE = 1;

?? ? private final int APP = 2;

?? ?

?? ? public void onCreate(Bundle savedInstanceState) {

?? ? ? ?super.onCreate(savedInstanceState);

?? ? ? //全屏显示

?? ? ? ? ? ?ActivityUtil.noNotificationBar(this);

?? ? ? ? ? ?ActivityUtil.noTitleBar(this);


?? ?setContentView(R.layout.addcard);

?? ? ? ? //转存出的路径

?? ? ? ? ? ?sdPhoto = new File("/sdcard/card_xx/photo_xx");

?? ? ? ? if(!sdPhoto.exists()){

?? ? ? ? sdPhoto.mkdirs();

?? ? ? ?}

//转存出的文件

?? ? ? ?sdCard = new File("/sdcard/card_xx/aa.png");

?? ? ? ?

?? ? ? ?add = (Button)findViewById(R.id.add);

?? ? ? ?take_p = (Button)findViewById(R.id.take_p);

?? ? ? ?app_ol = (Button)findViewById(R.id.app_ol);

?? ? ? ?

?? ? ? ?/* 事件处理 */

?? ? ? ?setListener();

?? ? ? ?

?? ? ? ?anim_alpha = new AlphaAnimation(0.5f, 0.5f);

anim_alpha.setDuration(80);

anim_alpha.setRepeatCount(1);

anim_alpha.setAnimationListener(new AnimationListener() {

public void onAnimationStart(Animation animation) {

}

public void onAnimationRepeat(Animation animation) {

}

public void onAnimationEnd(Animation animation) {

switch (menu) {

case LOCAL:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);?

intent.setType("image/*");

startActivityForResult(intent, 1);

break;

case TAKE:

? ?intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

? ?File out = new File(Environment.getExternalStorageDirectory(),"camera.png");

? ?Uri uri = Uri.fromFile(out);

? ?intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);


? ?startActivityForResult(intent, 3);?

break;

case APP:

intent = new Intent();

intent.setClass(AddCardActivity.this, TemplateListActivity.class);

startActivityForResult(intent, 9);

break;

}

}

});

?? ?}

?? ?

?? ?private void setListener()

?? ?{

?? ? /* 本地导入按钮事件处理 */

?? ? ? ?add.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

menu = LOCAL;

add.setAnimation(anim_alpha);

?? ? ? ? anim_alpha.startNow();

?? ? ? ? add.invalidate();

}

});

?? ? ? ?

?? ? ? ?/* 拍照 按钮事件处理 */

?? ? ? ?take_p.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

menu = TAKE;

take_p.setAnimation(anim_alpha);

?? ? ? ? anim_alpha.startNow();

?? ? ? ? take_p.invalidate();

}?

});

?? ? ? ?

?? ? ? ?/* 在线申请事件处理 */

?? ? ? ?app_ol.setOnClickListener(new OnClickListener()

?? ? ? ?{

?? ? ? ? public void onClick(View v)

?? ? ? ? {

?? ? ? ? menu = APP;

?? ? ? ? app_ol.setAnimation(anim_alpha);

?? ? ? ? anim_alpha.startNow();

?? ? ? ? app_ol.invalidate();

?? ? ? ? }

?? ? ? ?}

?? ? ? ?);

?? ? ? ?

?? ?}

?? ? ? ?/* 返回值处理 */

? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {

? ? if (resultCode == RESULT_OK){

? ? switch (requestCode) {

? ? /* 裁剪功能 ,裁剪后跳到2保存*/

case 1:

Intent cj = new Intent("com.android.camera.action.CROP");

cj.setData( data.getData());

cj.putExtra("crop", "true");

cj.putExtra("aspectX", 3); ?

cj.putExtra("aspectY", 2); ??

cj.putExtra("outputX",384 );

cj.putExtra("outputY", 256);

cj.putExtra("noFaceDetection", true); ?

cj.putExtra("return-data", true);?

startActivityForResult(Intent.createChooser(cj, "裁剪") , 2);

break;

/* 保存并跳转 */

case 2:

Intent intent = new Intent();

Bundle extras = data.getExtras();

if(extras != null ) { ?

? ?Bitmap photo = extras.getParcelable("data"); ?

? ?try {

FileOutputStream outStreamz = new FileOutputStream(sdCard);

photo.compress(Bitmap.CompressFormat.PNG, 50, outStreamz);

outStreamz.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

catch (IOException e) {

e.printStackTrace();

}

? ?intent.setClass(AddCardActivity.this,ChooseABActivity.class);

startActivity(intent);

finish();

}

break;

/* 拍照后保存图片,并跳到裁剪功能 ?*/

case 3:

Intent cj1 = new Intent("com.android.camera.action.CROP");

try {

cj1.setData(Uri.parse(android.provider.MediaStore.Images.Media.

insertImage(getContentResolver(),"/sdcard/camera.png", null, null)));

} catch (FileNotFoundException e) {

e.printStackTrace();

} ? ? ? ? ? ? ? ?

cj1.putExtra("crop", "true");

cj1.putExtra("aspectX", 3); ?

cj1.putExtra("aspectY", 2); ??

cj1.putExtra("outputX",384 );

cj1.putExtra("outputY", 256);

cj1.putExtra("return-data", true);?

startActivityForResult(cj1, 2);

File camera = new File("/sdcard/camera.png");

if(camera.exists())

?camera.delete();

break;

}

? ? }

? ? ?

? ? if (requestCode == 9) {

? ? Intent i=new Intent(this,MainMenuActivity.class);

? ? startActivity(i);

? ? finish();

? ? }

? ?}

? ??

? ? @Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

? ? ?

? ? if(keyCode==KeyEvent.KEYCODE_BACK)

? ? {

? ? Intent i=new Intent(this,MainMenuActivity.class);

? ? startActivity(i);

? ? finish();

? ? }

return super.onKeyDown(keyCode, event);

}?

? ??

}


/**************************************************************************************************************************************************/

如果照相之后要添加裁剪:

?class TakePicture implements OnClickListener{

??public void onClick(View v) {
???takePic();?

???File picture = new File("/sdcard/picture.jpg");
???Intent intent = new Intent("com.android.camera.action.CROP");?????

/**************如果不加这句话,会报错,找不到裁剪的Activity******/
?intent.setDataAndType( Uri.fromFile(file),"image/*);

/********************************************************************************/

???intent.putExtra("crop", "true");
???intent.putExtra("aspectX", 3);?
???intent.putExtra("aspectY", 2);??
???intent.putExtra("outputX",384 );
???intent.putExtra("outputY", 256);?
???intent.putExtra("return-data", true);
???startActivityForResult(intent, OVER);
???finish();
??}
??}

/***********************************************************************************************************************************************/

1 楼 yyj007 2011-09-18   您能给我提供一下这个例子的源代码吗?
我的邮箱是yyj.tom@gmail.com