Android - Cropping after camera and gallery intent gives strange results

I'm trying to fire android camera intent and photo select intent (two buttons, one to take, one to select from gallery) and both require an ultimatum fired after them, after which the cropped photo is returned to my app activity. I've gone through a bunch of examples posted elsewhere, but I'm getting weird results with my implementation.

For taking a photo shoot, it works fine, except after taking a photo and going into crop mode, the wrong photo appears. Instead of cropping the photo, you just took it to an old photo and I can't figure out where it came from. Also, sometimes after harvest intent completes, it crashes with a nullpointerexception thrown after a Parcel.readException (may not always be reproducible, but I think it pops up more if you take a shot and crop as quickly as possible).

For select photo selection, the gallery appears as expected, but on photo selection all that happens is a "Saved" message toasted instead of going back to my app action with the image. I believe I have a misunderstanding of how the Photo Select feature works (I pretty much used the code for the photography intent).

In both cases, in crop mode, you are still allowed to resize the cropped area, even though "scale" = false is specified.

My code looks like this:

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());
                    }
                }                   

            }
    }
}

}

      

Any help is greatly appreciated!

EDIT: I should also point out that I am testing LG Optimus LTE, Android 2.3

+3


source to share


1 answer


I think your problem is with the use of System.currentTimeMillis () on the temp filename. This sometimes explains getting an older file.

I would suggest just reusing the same temp file.



Hope it helps ...

0


source







All Articles