android编程实现系统图片剪裁的方法
本文实例讲述了android编程实现系统图片剪裁的方法。分享给大家供大家参考,具体如下:
packagecn.test;
importjava.io.File;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importandroid.app.Activity;
importandroid.content.ContentResolver;
importandroid.content.ContentUris;
importandroid.content.Intent;
importandroid.database.Cursor;
importandroid.graphics.Bitmap;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.provider.MediaStore;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
publicclassCutActivityextendsActivity{
privateButtonbutton;
privateImageViewimageView;
privateFilemCurrentPhotoFile;
privateBitmapcameraBitmap;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
imageView=(ImageView)findViewById(R.id.imageView);
button.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(
"android.media.action.IMAGE_CAPTURE");
mCurrentPhotoFile=newFile(
"mnt/sdcard/DCIM/Camera/",
getPhotoFileName());
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(mCurrentPhotoFile));
startActivityForResult(
intent,
Activity.DEFAULT_KEYS_DIALER);
}
});
}
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
switch(requestCode){
case1:
UriimgUri=MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolvercr=CutActivity.this
.getContentResolver();
UrifileUri=Uri.fromFile(mCurrentPhotoFile);
sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
fileUri));
try{
Thread.sleep(3000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
Cursorcursor=cr
.query(imgUri,null,
MediaStore.Images.Media.DISPLAY_NAME+"='"
+mCurrentPhotoFile.getName()+"'",
null,null);
Uriuri=null;
if(cursor!=null&&cursor.getCount()>0){
cursor.moveToLast();
longid=cursor.getLong(0);
uri=ContentUris.withAppendedId(imgUri,id);
}
finalIntentintent=newIntent(
"com.android.camera.action.CROP");
intent.setDataAndType(uri,"image/*");
intent.putExtra("crop","true");
intent.putExtra("outputX",380);
intent.putExtra("outputY",500);
intent.putExtra("return-data",true);
CutActivity.this.startActivityForResult(intent,3);
break;
case2:
break;
case3:
if(data!=null){
cameraBitmap=(Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
break;
default:
break;
}
super.onActivityResult(requestCode,resultCode,data);
}
privateStringgetPhotoFileName(){
Datedate=newDate(System.currentTimeMillis());
SimpleDateFormatdateFormat=newSimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
returndateFormat.format(date)+".jpg";
}
}
希望本文所述对大家Android程序设计有所帮助。