我正在尝试拍摄照片,然后对其进行裁剪。所以基本上,我启动相机意图,然后我开始裁剪意图。
由于我无法通过“onActivityResult”数据传递大位图,我创建了一个 URI 将图像直接保存到 SDCard,但由于某种原因,它被保存在模拟器上,而不是真实设备上。(我的意思是,CROP 没有保存在真实设备上。)
这是代码:
File resultingFile;
Bitmap fotoCreada;
Uri uriFoto;
相机意图:
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/PictureFolder/");
folder.mkdirs();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultingFile=new File(folder.toString() + "/"+currentDateandTime+".jpg");
this.uriFoto=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, this.uriFoto);
startActivityForResult(cameraIntent, 1888);
然后我检索结果:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//Si venim de la camera
if( requestCode == 1888 && resultCode == -1) { //-1 = TOT HA ANAT BE.
Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
Matrix matrix = new Matrix();
matrix.postRotate(90);
matrix.postScale(0.5f,0.5f);
Bitmap photoRotated = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
//Desem foto rotada.
this.fotoCreada=photoRotated;
try {
FileOutputStream out = new FileOutputStream(this.resultingFile.getPath());
this.fotoCreada.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
performCrop();
}
所以我打电话给 performCrop();
private void performCrop() {
try {
Log.d("debugging","Estic a crop. La foto és:"+this.uriFoto);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(this.uriFoto, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 600);
cropIntent.putExtra("outputY", 600);
//cropIntent.putExtra("return-data", true);
cropIntent.putExtra("ouput", this.uriFoto);
startActivityForResult(cropIntent,1234);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this.cont, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
如你看到的:
cropIntent.putExtra("ouput", this.uriFoto);
基本上告诉将作物输出到该 URI。URI 是正确的,因为我已经对其进行了调试(LogCat):
Estic 作物。照片:file:///mnt/sdcard/PictureFolder/20130123_223058.jpg
然后,我回到活动结果:
[... previous onactivityresult]
if (requestCode == 1234 && resultCode == -1){
Log.d("debugging","Ha acabat el croop.");
Uri imageUri = this.uriFoto;
try {
Bitmap photo = MediaStore.Images.Media.getBitmap(this.cont.getContentResolver(), imageUri);
this.fotoCreada=photo;
((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(this.fotoCreada);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
}
但我最终得到的是显示 (setImageBitmap(this.fotoCreada);) 从相机捕获的图像,而不是裁剪后的图片。这发生在真实设备上,但不在模拟器上!
我试图解释我能做到的最好的方式。随意问。
谢谢。