默认相机应用程序拍照时显示消息

时间:2013-11-22 03:35:59

标签: android

我想知道,一旦用户使用默认的相机应用程序捕获照片,我的应用程序是否可以显示消息?

所以基本上,当用户打开默认的相机应用程序并拍照时,我希望我的应用程序显示TOAST或消息(并且还可以访问此图像)。

我有什么方法可以做到吗?

由于

2 个答案:

答案 0 :(得分:2)

尝试使用它。

// 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);
}

并且在活动结果中:

    // 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();




        }

    }

答案 1 :(得分:0)

您可以使用如下的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 Manifest中,您只需要获取一些权限并使用intent过滤器注册您的接收器,并且适当的图像捕获操作也可以使您的接收器启用如下: -

<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

这可能会对你有帮助。