android - 如何借助广播接收器获取相机点击事件?

标签 android camera broadcastreceiver android-camera

我不想制作任何应用程序。我只想当我单击默认相机应用程序(附有屏幕截图)时,它通常会打开相机。然后,当我单击用于单击图像的按钮时,我只想显示 toast 。为此我尝试了一些东西。我只是试图接收广播 Intent 。代码如下。但我没有得到 toast 。请帮我。 enter image description here

MainActivity.java

public class MainActivity extends Activity {

    CameraReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent("com.android.camera.NEW_PICTURE");
        CameraReceiver myReceiver = new CameraReceiver();
        sendBroadcast(intent);
    }

    private class CameraReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            abortBroadcast();
            Log.d("New Photo Clicked", ">");
            Cursor cursor = context.getContentResolver().query(
                    intent.getData(), null, null, null, null);
            cursor.moveToFirst();
            String image_path = cursor
                    .getString(cursor.getColumnIndex("_data"));
            Toast.makeText(getApplicationContext(), "New Photo is Saved as : " + image_path,
                    1000).show();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        myReceiver = new CameraReceiver();
        IntentFilter i = new IntentFilter("android.intent.action.CAMERA_BUTTON");
        registerReceiver(myReceiver, i);
    }

    @Override 
    public void onResume() {
        IntentFilter i = new IntentFilter("android.intent.action.CAMERA_BUTTON");
        registerReceiver(myReceiver, i);
        super.onResume();
    }

    @Override 
    public void onPause() {
        unregisterReceiver(myReceiver);
        super.onPause();
    }
}

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.defaultcamers.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".CameraReceiver"
        android:enabled="true" >
        <intent-filter android:priority="10000">
            <action android:name="android.intent.action.CAMERA_BUTTON" />
            <action android:name="com.android.camera.NEW_PICTURE" />
            <action android:name="android.hardware.action.NEW_PICTURE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </receiver>
    <class android:name=".CameraReceiver" >
    </class>
</application>

现在,当我打开相机并尝试单击图片时,我会在“CameraReceiver.java”处收到“ClassNotFound Excaption”。

最佳答案

我通过将 CameraReceiver 类公开来解决了 ClassNotFoundException。我在我的 android 项目上实现了你的代码,并得到了 ClassNotFoundException 但我现在解决了它。只需按如下所示更改您的类,并像我在下面的代码中所做的那样更改 Toast 的第一个参数。

public class CameraReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub 
        abortBroadcast();
        Log.d("New Photo Clicked", ">");
        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();
    }

}

它应该有效,当我用相机拍照时,它对我有用。从 MainActivity 中删除该类,并为上述代码创建一个单独的公共(public) CameraReceiver 类。

关于android - 如何借助广播接收器获取相机点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24989221/

相关文章:

java - 无法设置互联网连接的广播接收器,如下是我的代码

android - 使用地理围栏时如何从 BroadcastReceiver 通知调用 Activity

java - Eclipse 使用旧的 PATH 变量在 Gradle 任务中执行命令行进程?

android - Flutter Camera Plugin 导致条形码阅读器崩溃

Android相机在某些设备中提供旋转捕获的图像

iphone - 如何将 "hang on"转换为直播中 iPhone 摄像头输入的两个不同点?

android - 确保屏幕方向固定为纵向且永不更改

java.lang.RuntimeException : Unable to instantiate activit

Android将视频保存到文件

Android:onResume() 跳转到另一个 Activity 而不是异常(exception)