Crop saving Bitmap
I'm trying to capture a photo, then crop it. So basically, I start camera Intent, then I start crop intent.
As I can't pass a big bitmap through "onActivityResult" data, I created a URI to save the image directly to SDCard, but for some reason, it's being saved on the emulator, but not on a real device. (I mean, the CROP isn't saved on the real device. )
This is the code:
File resultingFile;
Bitmap fotoCreada;
Uri uriFoto;
Camera Intent:
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);
Then I retrieve result:
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();
}
So I call 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();
}
}
As you can see:
cropIntent.putExtra("ouput", this.uriFoto);
Basically tells to Output the crop to that URI. The URI is correct, as I've debugged it (LogCat):
Estic a crop. La foto és: file:///mnt/sdcard/PictureFolder/20130123_223058.jpg
And then, I'm back to activity result:
[... 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());
}
But what I end up with, is displaying (setImageBitmap(this.fotoCreada);) the captured image from camera, but not the cropped picture. This happens on real device, but not on Emulator!
I tried to explain the best way I could. Feel free to ask.
Thanks.
链接地址: http://www.djcxy.com/p/36750.html上一篇: 禁用裁剪框架以在android中调整大小
下一篇: 裁剪节省位图