Как выбрать кусок изображения для установки аватара - PullRequest
0 голосов
/ 20 октября 2011

У меня есть приложение, в котором пользователь может установить свой аватар, сделав себе снимок или просто выбрав его из галереи. Я видел в других приложениях, что пользователь, после выбора изображения, отображал вид, где пользователь может «нарисовать» прямоугольник, выбирая, какую область изображения он хочет использовать в качестве аватара. Я хотел бы включить эту возможность в моем приложении. Как я могу сделать это после съемки?

Спасибо!

Edit:

Я пытаюсь сделать это с этим, но вместо камеры открывается галерея изображений:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.setType("image/*");
            cameraIntent.putExtra("crop", "true");
            cameraIntent.putExtra("scale", "true");
            cameraIntent.putExtra("outputX", 100);
            cameraIntent.putExtra("outputY", 100);
            cameraIntent.putExtra("aspectX", 1);
            cameraIntent.putExtra("aspectY", 1);
            cameraIntent.putExtra("max-width", 30);
            cameraIntent.putExtra("max-height", 30);
            cameraIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(cameraIntent, IMAGEN_CAMARA);    

1 Ответ

0 голосов
/ 20 октября 2011

Вот как я реализовал это в одном из моих приложений, которое использует эту функцию.Это довольно просто.

private void doTakePhotoAction() {
// http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo
// /1288723/kak-poluchit-url-zahvachennogo-izobrazheniya
// http://www.damonkohler.com/2009/02/android-recipes.html
// http://www.firstclown.us/tag/android/
// The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//Wysie_Soh: Create path for temp file
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                    "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

try {
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
    //Do nothing for now
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
    return;
}

switch (requestCode) {

case CROP_FROM_CAMERA: {
    //Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here
    //after the image is cropped.

    final Bundle extras = data.getExtras();

    if (extras != null) {
        Bitmap photo = extras.getParcelable("data");

        mPhoto = photo;
        mPhotoChanged = true;
        mPhotoImageView.setImageBitmap(photo);
        setPhotoPresent(true);
    }

    //Wysie_Soh: Delete the temporary file                        
    File f = new File(mImageCaptureUri.getPath());            
    if (f.exists()) {
        f.delete();
    }

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);

    break;
}

case PICK_FROM_CAMERA: {
    //After an image is taken and saved to the location of mImageCaptureUri, come here
    //and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setClassName("com.android.camera", "com.android.camera.CropImage");

    intent.setData(mImageCaptureUri);
    intent.putExtra("outputX", 96);
    intent.putExtra("outputY", 96);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);            
    startActivityForResult(intent, CROP_FROM_CAMERA);

    break;

}
}

}

Надеюсь, это поможет:)

...