Android監聽照相廣播
阿新 • • 發佈:2019-02-18
也許很多時候,你想實現一個功能,那就是,當用手機拍照時,你的程式可以知道已經拍照完成,並取得剛拍攝的照片。這個思路很簡單,就是新建一個廣播接收器,當手機進行拍照的時候,系統會發出一個廣播,你接受到此廣播,再通過intent傳來的資料進行解析,以取得圖片的位置。因為所有拍攝的照片都會在系統的一個content provider裡生成其對應的資料庫資訊,所以要用到ContentResolver進行解析。
一、特徵和許可權
<!-- 照相的特徵 -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<!-- 照相的許可權 -->
<uses-permission android:name="android.permission.CAMERA" />
二、新建廣播接收器
public class CameraReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Cursor cursor = context.getContentResolver().query(intent.getData(),
null, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex("_data"));
}
}
三、註冊廣播接收器
<!-- 照相的Receiver -->
<receiver
android:name="com.scnu.epdi.app.pictureshow.CameraReceiver"
android:enabled="true" >
<intent-filter android:priority="2147483647" >
<action android:name="android.hardware.action.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>
一、特徵和許可權
<!-- 照相的特徵 -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<!-- 照相的許可權 -->
<uses-permission android:name="android.permission.CAMERA" />
二、新建廣播接收器
public class CameraReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Cursor cursor = context.getContentResolver().query(intent.getData(),
null, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex("_data"));
}
}
三、註冊廣播接收器
<!-- 照相的Receiver -->
<receiver
android:name="com.scnu.epdi.app.pictureshow.CameraReceiver"
android:enabled="true" >
<intent-filter android:priority="2147483647" >
<action android:name="android.hardware.action.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>