Android获取系统拍照程序发出的广播

来源:互联网 发布:调查报告 知乎 编辑:程序博客网 时间:2024/06/05 10:55

说实话网上想找这段代码真的很少,我也是最近有这种需求才写的,公布出来以免更多人走弯路,节省大家的时间,话不多说请看代码

/** * @author  * 广播接收者 */public class CameraBroadCastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {System.out.println("谢霆锋,成龙,赵本山,刘德华:" + intent.getAction());Cursor cursor = context.getContentResolver().query(intent.getData(),null, null, null, null);cursor.moveToFirst();String image_path = cursor.getString(cursor.getColumnIndex("_data"));Toast.makeText(context, "New Photo is Saved as : -" + image_path,Toast.LENGTH_SHORT).show();}}

下面是对以上的广播接收者进行注册

        <!-- 广播注册 -->        <receiver            android:name=".CameraBroadCastReceiver"            android:enabled="true" >            <intent-filter android:priority="2147483647" >                <action android:name="com.android.camera.NEW_PICTURE" />                <action android:name="android.hardware.action.NEW_PICTURE" />                                <data android:mimeType="image/*" />            </intent-filter>        </receiver>
以上完成下面是测试结果:demo下载地址:Android获取系统拍照程序发出的广播



不过有个问题你们也见到了,接收者被调用了两次,这个问题你们自己解决吧,当然解决后,在评论里贴上代码最好啦,O(∩_∩)O哈哈~大笑

1 0
原创粉丝点击