GDK。使用CAMERA时,OnActivityResult()不会被触发

时间:2014-02-13 17:10:45

标签: android-intent android-camera google-glass

我正在按照GDK教程中的说明使用GDK试验Camera API,但我根本没有触发OnActivityResult API。我不确定为什么会这样,附上你的参考代码片段。感谢你的帮助。

// The "main" activity...
public class LiveCardDemoActivity extends Activity
{

    /* Camera section : Start */
    private void takePicture() {

        try
        {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_PICTURE_REQUEST);

            Log.d("takePicture() is complete...");
        }
        catch ( Exception ex)
        {
            Log.d("takePicture() thrown an exception :"+ex.getMessage());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
            String picturePath = data.getStringExtra(
                    CameraManager.EXTRA_PICTURE_FILE_PATH);
            processPictureWhenReady(picturePath);
        }

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

    private void processPictureWhenReady(final String picturePath) {

        Log.d("Yayyyyyyyyyy.... Camera is working...");

        displayProgress("Activating STB ...",R.drawable.menu_icon_record);
    }

   @Override
    protected void onDestroy()
    {
        //doUnbindService(); -- GD
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.d("Beta: onCreate() called.");

        createCards();
        mCardScrollView = new CardScrollView(this);
        m_viewAdapter = new ExampleCardScrollAdapter();
        mCardScrollView.setAdapter(m_viewAdapter);
        mCardScrollView.activate();

        setContentView(mCardScrollView);

        /* -- GD
        setContentView(R.layout.activity_livecarddemo);
        */

        // For gesture handling.
        mGestureDetector = createGestureDetector(this);

        // doStartService(); --GD
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        Log.d("Beta: onResume() called.");

        // For live card menu handling
        //openOptionsMenu();  --GD
    }

    // Context menus
    // ...

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        Log.d("onCreateOptionsMenu() called.");

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_livecarddemo_livecard, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        Log.d("onOptionsItemSelected() called.");

        // Handle item selection.
        switch (item.getItemId()) {
            case R.id.menu_item_Direction:
                Navigate();
                break;
            case R.id.menu_item_Scan:
                scan_Codes();
                break;
            case R.id.menu_item_Activate:
                takePicture();
                break;
            default:
                 break;
        }

        //return super.onOptionsItemSelected(item);
        return true;
    }


   @Override
    public void onOptionsMenuClosed(Menu menu)
    {
        Log.d("onOptionsItemSelected() called.");

        // Nothing else to do, closing the activity.
        finish();
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (mGestureDetector != null) {
            return mGestureDetector.onMotionEvent(event);
        }
        return false;
    }

    private GestureDetector createGestureDetector(Context context) {
        GestureDetector gestureDetector = new GestureDetector(context);
        // Create a base listener for generic gestures
        gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
            @Override
            public boolean onGesture(Gesture gesture) {
                if (Log.D)
                    Log.d("gesture = " + gesture);
                /*if(gesture == Gesture.SWIPE_RIGHT)
                {
                    scrollLiveCards();
                    return true;
                } */

                 if (gesture == Gesture.TAP || gesture == Gesture.LONG_PRESS) {
                    handleGestureTap();
                    return true; 
                } else if (gesture == Gesture.TWO_TAP) {
                    handleGestureTap();
                    //handleGestureTwoTap();
                    return true;
                }
                return false;
            }
        });
        return gestureDetector;
    }

    private void handleGestureTap() {
        Log.d("handleGestureTap() called.");
        //doStopService(); -- GD
        //finish(); -- GD
        openOptionsMenu();
    }

}

1 个答案:

答案 0 :(得分:0)

我有app在onCreate中调用两行代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);

和onActivityResult被调用没有问题。因此,您可能希望直接在onCreate中尝试代码。

另外,请确保您的AndroidManifest.xml包含以下内容:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />