仅在按下后退按钮后调用onActivityResult

时间:2016-04-10 12:56:17

标签: android android-studio

我现在正在用android学习编程,我构建了一个应用程序,用本教程用相机拍照。

http://developer.android.com/training/camera/photobasics.html

但onActivity不会自动调用,只有在我用设备拍照后按下后退按钮时才会调用它。

/**
 * This utility class draws and scales an image to fit canvas of a component.
 * if the image is smaller than the canvas, it is kept as it is.
 *
 * @author www.codejava.net
 *
 */
public class ImageDrawer {

    public static void drawScaledImage(Image image, Component canvas, Graphics g) {
        int imgWidth = image.getWidth(null);
        int imgHeight = image.getHeight(null);

        double imgAspect = (double) imgHeight / imgWidth;

        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();

        double canvasAspect = (double) canvasHeight / canvasWidth;

        int x1 = 0; // top left X position
        int y1 = 0; // top left Y position
        int x2 = 0; // bottom right X position
        int y2 = 0; // bottom right Y position

        if (imgWidth < canvasWidth && imgHeight < canvasHeight) {
            // the image is smaller than the canvas
            x1 = (canvasWidth - imgWidth)  / 2;
            y1 = (canvasHeight - imgHeight) / 2;
            x2 = imgWidth + x1;
            y2 = imgHeight + y1;

        } else {
            if (canvasAspect > imgAspect) {
                y1 = canvasHeight;
                // keep image aspect ratio
                canvasHeight = (int) (canvasWidth * imgAspect);
                y1 = (y1 - canvasHeight) / 2;
            } else {
                x1 = canvasWidth;
                // keep image aspect ratio
                canvasWidth = (int) (canvasHeight / imgAspect);
                x1 = (x1 - canvasWidth) / 2;
            }
            x2 = canvasWidth + x1;
            y2 = canvasHeight + y1;
        }

        g.drawImage(image, x1, y1, x2, y2, 0, 0, imgWidth, imgHeight, null);
    }

3 个答案:

答案 0 :(得分:1)

ACTION_IMAGE_CAPTURE的行为取决于相机应用。有数千种设备型号,配有数百种不同的相机应用程序。此外,还有可从Play商店和其他地方下载的相机应用程序。

有些人有错误。

在这种情况下,ACTION_IMAGE_CAPTURE 在用户拍照后返回您的应用。无论出于何种原因,这个相机应用程序的实施者选择不这样做,或者没有很好地测试ACTION_IMAGE_CAPTURE

除了不使用ACTION_IMAGE_CAPTURE之外,您无法真正做到这一点(例如,直接使用相机API)。

答案 1 :(得分:0)

onActivityResult()在您呼叫的活动关闭时调用,以此预期行为。

按“返回”键可关闭活动,以便传送结果。

您无法从仍处于打开状态的活动中获得结果。

答案 2 :(得分:0)

如果这是您无法触及的限制,那么最好的选择是创建自定义活动以通过相机捕获。 Here是一个如何完成它的例子。我没有测试过。但是,你应该能够弄明白,因为它似乎并不复杂。