本文实例讲述了android编程实现系统图片剪裁的方法。分享给大家供大家参考,具体如下:
package cn.test;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CutActivity extends Activity {
private Button button;
private ImageView imageView;
private File mCurrentPhotoFile;
private Bitmap cameraBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
mCurrentPhotoFile = new File(
"mnt/sdcard/DCIM/Camera/",
getPhotoFileName());
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile));
startActivityForResult(
intent,
Activity.DEFAULT_KEYS_DIALER);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = CutActivity.this
.getContentResolver();
Uri fileUri = Uri.fromFile(mCurrentPhotoFile);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
fileUri));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Cursor cursor = cr
.query(imgUri, null,
MediaStore.Images.Media.DISPLAY_NAME + "='"
+ mCurrentPhotoFile.getName() + "'",
null, null);
Uri uri = null;
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToLast();
long id = cursor.getLong(0);
uri = ContentUris.withAppendedId(imgUri, id);
}
final Intent intent = new Intent(
"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;
case 2:
break;
case 3:
if (data != null) {
cameraBitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
}
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android 开发随手笔记之使用摄像头拍照Android调用手机拍照功能的方法android 拍照和上传的实现代码android系统在静音模式下关闭camera拍照声音的方法Android拍照保存在系统相册不显示的问题解决方法android图像绘制(六)获取本地图片或拍照图片等图片资源Android实现读取相机(相册)图片并进行剪裁android照相、相册获取图片剪裁报错的解决方法Android编程实现图片拍照剪裁的方法