裁剪节省位图
我正在尝试拍摄一张照片,然后裁剪。 所以基本上,我启动相机意图,然后我开始作物意图。
由于我无法通过“onActivityResult”数据传递大的位图,因此我创建了一个将图像直接保存到SDCard的URI,但由于某种原因,它将保存在仿真器上,但不保存在真实设备上。 (我的意思是,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);)从摄像头捕获的图像,但不是裁剪的图像。 这发生在真实设备上,但不在仿真器上!
我试图解释我能做到的最好方式。 随意问。
谢谢。
链接地址: http://www.djcxy.com/p/36749.html上一篇: Crop saving Bitmap