我在想,當用戶使用默認相機應用程序拍攝照片時,我的應用程序是否可以顯示消息?因此,基本上,當用戶打開默認相機應用程序並拍攝照片時,我希望我的應用程序顯示TOAST或消息(並且還可以訪問此圖像)。默認相機應用程序拍攝照片時顯示一條消息
有什麼辦法可以做到嗎?
感謝
我在想,當用戶使用默認相機應用程序拍攝照片時,我的應用程序是否可以顯示消息?因此,基本上,當用戶打開默認相機應用程序並拍攝照片時,我希望我的應用程序顯示TOAST或消息(並且還可以訪問此圖像)。默認相機應用程序拍攝照片時顯示一條消息
有什麼辦法可以做到嗎?
感謝
您可以使用FileObserver象下面這樣: -
FileObserver observer = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA") { // set up a file observer to watch this directory on sd card
@Override
public void onEvent(int event, String file) {
if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
Log.d(TAG, "File created [" + android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA/" + file + "]");
fileSaved = "New photo Saved: " + file;
}
}
};
observer.startWatching(); // start the observer
您還可以使用CameraEventReciver: -
創建一個接收器你的代碼說CameraEventReciver,你可以實現你的代碼,如: -
public class CameraEventReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
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, 1000).show();
}
}
而在Android清單你必須要採取一些權限,並註冊意圖過濾並採取適當行動的圖像捕獲你的reciever也使能喜歡你的接收器的android: -
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<receiver
android:name="com.android.application.CameraEventReciver"
android:enabled="true" >
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>
更多信息,採取一看: -
broadcast receiver won't receive camera event
這可能會幫助你。
真棒謝謝:)接受 – Snake
嘗試使用此。
// method to take picture from camera
protected void takepicture() {
// TODO Auto-generated method stub
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
values.clear();
startActivityForResult(intent, 5);
}
和activiyt結果:
// on activity result we store captures image in images
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 5) {
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(mCapturedImageURI, projection,
null, null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
image_path = cursor.getString(column_index_data);
Log.e("path of image from CAMERA......******************.........",
image_path + "");
// bitmap = BitmapFactory.decodeFile(image_path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeFile(image_path, options);
Toast.makeText(getApplicationcontext, "photo taken", 1000)
.show();
}
}
謝謝你的回答,但這會打開/捕獲我的應用程序內的圖像。我不想那樣。我想要的是,當用戶啓動默認相機應用程序,他們拍照,不知何故我的應用程序被通知這個,所以我可以顯示msg – Snake
發送你試過的代碼 – Invader
我不知道從哪裏開始。我搜索了很多,但無法找到類似的問題 – Snake
這在SO上被問過很多次,簡短回答是否定的,除非您的應用程序始終在後臺運行,並且在相機文件夾中偵聽更改和/或媒體商店。即使對任何一個進行更改也不一定意味着拍攝了新照片。 –