public class BeginTakePhotoScene extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_begin_take_photo_scene);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
takephotobtn = new ImageButton(this);
takephotobtn.setId(888);
takephotobtn.setBackgroundResource(R.drawable.take_photo_btn);
takephotobtnparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
takephotobtnparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
takephotobtnparam.topMargin = margin.heightPixels * 9 / 10 - height;
takephotobtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), String.valueOf(System
.currentTimeMillis()) + ".jpg"));
camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
camera.putExtra("return-data", false);
startActivityForResult(camera, PICTURE_RESULT);
}
});
choosephotobtn = new ImageButton(this);
choosephotobtn.setId(889);
choosephotobtn.setBackgroundResource(R.drawable.choose_photo_btn);
choosephotobtnparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
choosephotobtnparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
choosephotobtnparam.addRule(RelativeLayout.BELOW, 888);
choosephotobtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
selectedImageUri = data.getData();
uncroppedimage = getFilePath(selectedImageUri);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(selectedImageUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
File tempFile;
tempFile = new File(Environment.getExternalStorageDirectory(),
String.valueOf(System.currentTimeMillis()) + ".jpg");
mSavedUri = Uri.fromFile(tempFile);
cropIntent.putExtra("output", mSavedUri);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} else if (requestCode == PICTURE_RESULT) {
Log.e("mUri", mUri.getPath());
uncroppedimage = mUri.getPath();
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(mUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
File tempFile;
tempFile = new File(Environment.getExternalStorageDirectory(),
String.valueOf(System.currentTimeMillis()) + ".jpg");
mSavedUri = Uri.fromFile(tempFile);
cropIntent.putExtra("output", mSavedUri);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} else {
Log.e("mSavedUri", mSavedUri.getPath());
profileimage.setImageURI(mSavedUri);
croppedimage = mSavedUri.getPath();
profileimage.setVisibility(View.VISIBLE);
confirmbtn.setVisibility(View.VISIBLE);
takephotobtnparam.topMargin = margin.heightPixels * 9 / 10
- height * 2;
}
}
}
private String getFilePath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = this.managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int colum_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(colum_index);
} else
return null;
}
}
我记录uri.getpath
,它返回file:///mnt/sdcard/1358731920220.jpg
。这个链接似乎有额外的/
。然而,我不知道为什么会这样?
cropIntent.putExtra("return-data", true);
改为
cropIntent.putExtra("return-data", false);
将裁剪后的图像保存到sdcard.
在Manifest文件中添加此权限,如果您还没有添加,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />