2011-10-12 70 views
1
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甚至沒有被調用)。 selectedImage變量中有一個真實的圖像,所以問題不在這裏。Android 3.x圖像作物冷凍(摩托羅拉xoom和acer iconia)

在3.1和3.2的android模擬器上,應用程序也可以很好地工作。 有誰知道這有什麼問題?

回答

2

試試這個方法,它爲我工作。它不是我的,我在這裏成立於計算器:How to crop Bitmap Center like imageview?

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; 
} 
-1

嘗試通過intent.setDataAndType(selectedImage, "image/*");

更換 intent.setData(selectedImage);
相關問題