不为MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA调用onActivityResult

时间:2011-07-20 07:54:14

标签: android

我正在尝试使用MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA调用相机。 但是,使用StartActivityForResult不会调用onActivityResult

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

// get image count in media store befor start camera  Start camera with chooser 
public void startCameraActivity() {
        Cursor cursor = loadCursor();

        image_count_before = cursor.getCount();

        cursor.close();

        Intent cameraIntent = new Intent(
                MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
        cameraIntent = Intent.createChooser(cameraIntent, "Select Camera");
        List<ResolveInfo> activities = getPackageManager()
                .queryIntentActivities(cameraIntent, 0);
        if (activities.size() > 0)
            startActivityForResult(cameraIntent, CAPTURE_IMAGES_FROM_CAMERA);
        else
            Toast.makeText(this, "No Camera application", Toast.LENGTH_SHORT)
                    .show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {

        case CAPTURE_IMAGES_FROM_CAMERA:

            exitingCamera();
            break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public Cursor loadCursor() {

        final String[] columns = { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID };

        final String orderBy = MediaStore.Images.Media.DATE_ADDED;

        return getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy);
    }

    public String[] getImagePaths(Cursor cursor, int startPosition) {

        int size = cursor.getCount() - startPosition;

        if (size <= 0)
            return null;

        String[] paths = new String[size];

        int dataColumnIndex = cursor
                .getColumnIndex(MediaStore.Images.Media.DATA);

        for (int i = startPosition; i < cursor.getCount(); i++) {

            cursor.moveToPosition(i);

            paths[i - startPosition] = cursor.getString(dataColumnIndex);
        }

        return paths;
    }

    private void exitingCamera() {

        Cursor cursor = loadCursor();
        String[] paths = getImagePaths(cursor, image_count_before);
        cursor.close();
        new ProcessImage(paths).execute();

    }