开发者

Android camera intents

Roger,

I see that you've been tinkering with camera intents. I'm having real trouble ju开发者_StackOverflowst getting a simple app to tell me when the camera button has been pressed. Do you have some code to help me on my way.

Thanks.

David


In the manifest, you need to state you want to receive the intent for the camera button:

    <receiver android:name="domain.namespace.CameraReceiver">
        <intent-filter>
            <action android:name="android.intent.action.CAMERA_BUTTON"/>
        </intent-filter>
    </receiver>
    <activity android:name="domain.namespace.MyCameraActivity"
            android:label="@string/app_name" android:screenOrientation="landscape" android:icon="@drawable/camera"
        android:clearTaskOnLaunch="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    <intent-filter>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

In the receiver:

public void onReceive(Context context, Intent intent) {
  KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

  if (event == null) {
    return;
  }

  //prevent the camera app from opening
  abortBroadcast();    

  Intent i = new Intent(Intent.ACTION_MAIN);
  i.setClass(context, MyCameraActivity.class);
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(i);    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜