启动使用相机

时间:2018-03-24 14:47:03

标签: java android android-intent android-camera

我找到了一些开源文档扫描程序应用程序,首先(默认)" DocumentScannerActivity"该应用程序是用于捕获文档图像以供进一步处理的相机。效果很好,没有任何错误。

然后我添加了自己的(新)" MainActivity"默认情况下,按钮位于内部, onclick listener 打开" DocumentScannerActivity":

        scanActivityBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DocumentScannerActivity.class);
                startActivity(intent);
            }
        });

此代码打开" DocumentScannerActivity",但按下"捕获图像"应用程序崩溃错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
                                                                           at az.deep.archiverabitabank.DocumentScannerActivity.saveDocument(DocumentScannerActivity.java:1042)
                                                                           at az.deep.archiverabitabank.DocumentScannerActivity.saveDocument(DocumentScannerActivity.java:1129)
                                                                           at az.deep.archiverabitabank.ImageProcessor.processPicture(ImageProcessor.java:161)
                                                                           at az.deep.archiverabitabank.ImageProcessor.handleMessage(ImageProcessor.java:91)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                           at android.os.Looper.loop(Looper.java:172)
                                                                           at android.os.HandlerThread.run(HandlerThread.java:65)

以下是 DocumentScannerActivity 中代码中有问题的部分(应用尝试将捕获的图像保存到存储中):

    Uri fileUri = null;
    if (intent.getAction().equals("android.media.action.IMAGE_CAPTURE")) {
        fileUri = ((Uri) intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT));
        Log.d(TAG, "intent uri: " + fileUri.toString());  //LINE 1042, where error occurs
        try {
            fileName = File.createTempFile("onsFile", ".jpg", this.getCacheDir()).getPath();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        isIntent = true;
    }

以下是来自 AndroidManifest.xml 的相关剪辑:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
...
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DocumentScannerActivity"
        android:label="@string/title_activity_document_scanner"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="portrait"
        android:theme="@style/FullscreenTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

我做错了什么,如何防止此错误?

0 个答案:

没有答案