【问题标题】:Display a message when the default camera app takes a picture当默认相机应用拍照时显示消息
【发布时间】:2013-11-22 03:35:59
【问题描述】:

我想知道,有没有一种方法可以在用户使用默认相机应用拍摄照片后立即显示消息?

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

有什么办法可以做到吗?

谢谢

【问题讨论】:

  • 发送你试过的代码
  • 我不知道从哪里开始。我搜索了很多,但找不到类似的问题
  • 这已经在 SO 上被问过很多次,简短的回答是否定的,除非您的应用程序始终在后台运行并监听相机文件夹和/或媒体存储的更改。即使这样,对其中任何一个的更改也不一定意味着拍摄了一张新照片。

标签: android


【解决方案1】:

尝试使用它。

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




        }

    }

【讨论】:

  • 感谢您的回答,但这将在我的应用程序中打开/捕获图像。我不想要那个。我想要的是,当用户启动默认相机应用程序并拍照时,我的应用程序会以某种方式收到通知,以便我可以显示一条消息
【解决方案2】:

你可以像下面这样使用 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 过滤器注册您的接收器,并且适当的图像捕获操作还可以使您的接收器 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

这可能会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多