如何在广播接收器的帮助下获得相机点击事件? [英] How to get camera click event with the help of broadcast receiver?

查看:329
本文介绍了如何在广播接收器的帮助下获得相机点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想制作任何应用程式。我只想要当我点击默认的相机应用程序(屏幕截图是附加的),然后它通常打开相机。然后当我点击按钮点击图像,然后我只想显示一个吐司。为此,我尝试了一些东西。我只是试图接收广播意图。代码如下。但我没有得到吐司。请帮帮我。

I don't want to make any app. I just want that when I click on the default camera app(screen shot is attached), then it normally open the camera. Then as I'll click the button for clicking the image, then I just want to show A Toast. For this I tried something. I just tried to receive the the broadcast Intent. The code is follows. But I am not getting the Toast. Please help me.

MainActivity .java

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

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>

现在当我打开相机并试图点击图片时,我得到ClassNotFound Excaption CameraReceiver.java。

Now when I am opening camera and trying to click the pic then I am getting "ClassNotFound Excaption" at "CameraReceiver.java".

推荐答案

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

I solved the ClassNotFoundException by making the CameraReceiver class public. I implemented your code on my android project, and got the ClassNotFoundException but i solved it now. Just change your class as following and also change the first parameter of Toast as I did in the following code.

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中删除类,并为上面的代码创建一个单独的公共CameraReceiver类。

and it should work, it is working for me, when I take picture from camera. Remove the class from MainActivity and create a separate public CameraReceiver class for above code.

这篇关于如何在广播接收器的帮助下获得相机点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆