Android - 在相机和图库意图之后裁剪会产生奇怪的结果

时间:2012-03-14 23:38:28

标签: java android android-intent android-camera android-gallery

我正在尝试启动Android相机意图和选择照片意图(两个按钮,一个用于拍照,一个用于选择图库)并且两者都需要在它们之后启动裁剪意图然后将裁剪的照片返回到我的应用程序的活动。我已经浏览了一些在其他地方发布的例子,但是我对我的实现感到奇怪。

对于拍摄照片事件,它似乎工作正常,除了在拍摄照片并进入裁剪模式后,弹出错误的照片。你没有裁剪照片,而是拍摄了一张较旧的照片,我无法弄清楚它的来源。此外,有时在完成裁剪意图后,它会在Parcel.readException之后崩溃并返回nullpointerexception(不能总是重现,但我认为如果您拍摄照片并尽可能快地裁剪它会更多)。

对于选择照片意图,您的图库会按预期弹出,但在选择照片时,所有发生的事情都是“保存”消息,而不是使用图像返回到我应用的活动。我相信我对选择照片意图的工作原理有误解(我几乎重复使用代码拍摄照片意图)。

在这两种情况下,在裁剪模式下,尽管指定了“scale”= false,仍然可以调整裁剪区域的大小。

我的代码如下:

public class TestPhotoActivity extends Activity {

private ImageView imageView;
private Uri imageUri;

private int int_Height_crop = 600;
private int int_Width_crop = 600;

public final static int TAKE_PICTURE = 0;
public final static int CHOOSE_PICTURE = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.choose_photo); 

    imageView = (ImageView) findViewById(R.id.photo);

    Button take_photo = (Button) findViewById(R.id.take_photo);
    take_photo.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View view) {  
            takePhoto(view);
        }
    });

    Button choose_photo = (Button) findViewById(R.id.choose_photo);
    choose_photo.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View view) {  
            choosePhoto(view);
        }
    });     

}

public void takePhoto(View view) {      
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE", null);  
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", int_Width_crop);
    intent.putExtra("outputY", int_Height_crop);
    intent.putExtra("scale", false);
    intent.putExtra("return-data", true);
    File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");      
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);
    startActivityForResult(intent, TAKE_PICTURE);
}

public void choosePhoto(View view) {        
    Intent intent = new Intent(Intent.ACTION_PICK, null); 
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", int_Width_crop);
    intent.putExtra("outputY", int_Height_crop);
    intent.putExtra("scale", false);
    intent.putExtra("return-data", true);
    File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + "Pic.jpg");        
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    startActivityForResult(intent, CHOOSE_PICTURE);         
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {          
        case TAKE_PICTURE:              
            Log.d("photo", "requestCode: " + requestCode + "resultCode: " + resultCode + "wanted result: " + Activity.RESULT_OK);               
            if (resultCode == Activity.RESULT_OK) {

                if (data == null) {
                    Log.w("photo", "Null data, but RESULT_OK, from image picker!");
                    Toast t = Toast.makeText(this, "No photo picked.", Toast.LENGTH_SHORT);
                    t.show();
                    return;
                }

                final Bundle extras = data.getExtras();

                if (extras != null) {   
                    Log.d("photo", "extras is not null");
                    Uri selectedImage = imageUri;
                    getContentResolver().notifyChange(selectedImage, null);                
                    ContentResolver cr = getContentResolver();
                    Bitmap bitmap;
                    try {
                        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);     
                        Log.d("photo", "data.getAction() is not null. setting image.");
                        imageView.setImageBitmap(bitmap);                           
                    } catch (Exception e) {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                        Log.e("photo", e.toString());
                    }
                }
            }
        case CHOOSE_PICTURE:
            Log.d("photo", "requestCode: " + requestCode + "resultCode: " + resultCode + "wanted result: " + Activity.RESULT_OK);
            if(resultCode == RESULT_OK){
                Log.d("photo", "resultCode is ok");

                if (data == null) {
                    Log.w("photo", "Null data, but RESULT_OK, from image picker!");
                    Toast t = Toast.makeText(this, "No photo picked.", Toast.LENGTH_SHORT);
                    t.show();
                    return;
                }

                final Bundle extras = data.getExtras();

                if (extras != null) {   
                    Log.d("photo", "extras is not null");
                    Uri selectedImage = imageUri;
                    getContentResolver().notifyChange(selectedImage, null);                
                    ContentResolver cr = getContentResolver();
                    Bitmap bitmap;
                    try {
                        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);  
                        Log.d("photo", "data.getAction() is not null. setting image.");
                        imageView.setImageBitmap(bitmap);
                    } catch (Exception e) {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                        Log.e("photo", e.toString());
                    }
                }                   

            }
    }
}

}

非常感谢任何帮助!

编辑:我还应该注意到我正在测试LG Optimus LTE,Android 2.3

1 个答案:

答案 0 :(得分:0)

我认为您的问题来自于在临时文件名中使用System.currentTimeMillis()。这可以解释有时获取旧文件。

我建议重新使用相同的临时文件。

希望它有所帮助...