Заморозка изображений в Android 3.x (Motorola Xoom и Acer Iconia) - PullRequest
1 голос
/ 12 октября 2011
Intent intent = new Intent("com.android.camera.action.CROP");

File path = this.getExternalFilesDir("tmp");
File file = new File(path, "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
Uri tmpUri = Uri.fromFile(file);

intent.setData(selectedImage);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("crop", "true");
intent.putExtra("scale", "true");
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);

intent.putExtra("return-data", false);        
startActivityForResult(intent, REQUEST_CROP);

Я использую этот код для обрезки изображения. Он отлично работает на Android 2.x. Но на 3.1 (motorola xoom) и 3.2 (acer iconia) приложение зависает после выбора области обрезки и нажатия «Сохранить» (onActivityResult даже не вызывается). Существует переменная Uri для реального изображения в переменной selectedImage, поэтому проблема не в этом.

На эмуляторах андроида 3.1 и 3.2 приложение тоже отлично работает. Кто-нибудь знает в чем проблема?

Ответы [ 3 ]

2 голосов
/ 24 октября 2012

Попробуйте этот метод, он работал для меня.Это не мое, я основал здесь в stackoverflow: Как обрезать Bitmap Center, как просмотр изображений?

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger 
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the new, scaled version of the source bitmap will now
    // be
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    // Finally, we create a new bitmap of the specified size and draw our new,
    // scaled bitmap onto it.
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
    Canvas canvas = new Canvas(dest);
    canvas.drawBitmap(source, null, targetRect, null);

    return dest;
}
2 голосов
/ 05 марта 2012

Проблема, вероятно, в том, что начался урожай.Функция «crop» = «true» extra не гарантируется для всех устройств, поскольку она неофициальная.

Подробнее см. В этой теме: http://groups.google.com/group/android-developers/browse_frm/thread/2dd647523926192c/4b6d087073a39607?tvc=1#4b6d087073a39607

0 голосов
/ 28 марта 2014

Попробуйте заменить intent.setData(selectedImage); на intent.setDataAndType(selectedImage, "image/*");

...