我正在尝试启动一个android相机意图和一个选择照片意图(两个按钮,一个用于拍照,一个用于从图库中选择),并且都需要在它们之后启动一个裁剪意图,然后将裁剪的照片返回到我的应用程序活动中。我已经看过了很多贴在其他地方的例子,但是我的实现有了奇怪的结果。
对于拍照事件,它似乎工作正常,除了拍照并进入裁剪模式后,会弹出错误的照片。你拍的照片不是裁剪的,而是裁剪了一张旧照片,我不知道它是从哪里来的。此外,有时在完成裁剪意图之后,它会在parcel.readexception之后由于nullpointereexception而崩溃(不能总是复制,但我认为如果您拍摄一张照片并尽快裁剪,它会出现更多问题)。
对于“选择照片”的意图,您的图库会按预期弹出,但在选择照片时,所发生的一切都是“保存”的消息将被烤熟,而不是返回到我的应用程序的活动与图像。我相信我对choose photo intent的工作原理有一个误解(我几乎重用了拍照意图的代码)。
在这两种情况下,在裁剪模式下,尽管指定了“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

最佳答案

我认为您的问题来自于在临时文件名中使用system.currentTimeMillis()。这可以解释为什么有时会得到一个旧文件。
我建议重复使用同一个临时文件。
希望有帮助…

10-08 09:08
查看更多