简体   繁体   English

无法通过默认相机应用程序的意图拍摄照片

[英]Couldn't take a pic via an intent from default camera app

I have three classes. 我有三节课。 One parent class where button listeners are defined. 一个父类,其中定义了按钮侦听器。 Second class is for capturing pic and third class gives the path to the pic. 第二类用于捕获图片,第三类提供通往图片的路径。 However I am facing certain troubles. 但是,我面临一些麻烦。

Startingactivity.java Startingactivity.java

 package xxxx.xxxx.xxxx;

import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class StartingActivity extends AppCompatActivity {







@Override
protected void onCreate(Bundle savedInstanceState) {
    ActionBar actionBar=getActionBar();
    //actionBar.setTitle();          Put path name here.
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

            new CreateFolder();
   final Captureit captureit=new Captureit();

    Button add,pic,vid,settings;
    add=(Button)findViewById(R.id.addbutton);
    pic=(Button)findViewById(R.id.picbutton);
    vid=(Button)findViewById(R.id.vidbutton);
    settings=(Button)findViewById(R.id.setbutton);


    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                captureit.capturepicture();
            }
        });
        vid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                captureit.capturevideo();
            }
        });

    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

 }
 }

Captureit.java Captureit.java

package sagar.mehar.camera2;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;



public class Captureit extends AppCompatActivity{

private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE=200;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE=100;
public static final int MEDIA_TYPE_IMAGE=1;
public static final int MEDIA_TYPE_VIDEO=2;
private Uri fileUri;
public static CreateFolder createFolder=new CreateFolder();
Intent intent;




protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        if(resultCode==RESULT_OK)
        {//Image Captured
            Toast.makeText(this, "Image Captured ", Toast.LENGTH_SHORT).show();
        }
        else if(resultCode==RESULT_CANCELED)
        {//User Cancelled Image capture

        }
        else
        {//Image capture failed.
            Toast.makeText(this,"Image Captured Failed",Toast.LENGTH_SHORT).show();
        }
    }
    if(requestCode==CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE)
    {
        if(resultCode==RESULT_OK)
        {
            Toast.makeText(this,"Video Captured",Toast.LENGTH_SHORT).show();
        }
        else if(resultCode==RESULT_CANCELED)
        {

        }else
        {//Video Capture Failed.
            Toast.makeText(this,"Video Captured Failed",Toast.LENGTH_SHORT).show();
        }
    }
}



public void capturepicture() {

    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
    this.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}




public void capturevideo()
{
    intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    fileUri=getOutputMediaFileUri(MEDIA_TYPE_VIDEO);//Create a file to save the video
    intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
    startActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}
private static Uri getOutputMediaFileUri(int type)
{
    return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type)
{
    String timeStamp=new SimpleDateFormat("yyyyMMdd__HHmmss").format(new Date());
    File mediaFile;
    if(type==MEDIA_TYPE_IMAGE)
    {
        mediaFile=new File(createFolder.gettPath()+File.separator+"IMG_"+timeStamp+".jpg");
    }
    else if(type==MEDIA_TYPE_VIDEO)
    {
        mediaFile=new File(createFolder.gettPath()+File.separator+"VID_"+timeStamp+".mp4");
    }
    else
    {
        return null;
    }
    return mediaFile;
}

}

Third Class is working properly. 三等舱工作正常。

Manifest code is: 清单代码是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxxx.xxxx.xxxx"
>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera2"/>
<uses-permission android:name="android.hardware.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".StartingActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

However, whenever I am clicking button to take a pic, app always crashes giving the following log. 但是,每当我单击按钮拍照时,应用程序始终崩溃,并显示以下日志。

01-03 11:01:27.568 11291-11291/xxxx.xxxx.xxxx W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b9c98)
01-03 11:01:27.568 11291-11291/xxxx.xxxx.xxxx W/dalvikvm: threadid=1: uncaught exception occurred
01-03 11:01:27.568 11291-11291/xxxx.xxxx.xxxx W/System.err: java.lang.NullPointerException
01-03 11:01:27.569 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:3457)
01-03 11:01:27.569 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:3418)
01-03 11:01:27.570 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
01-03 11:01:27.570 11291-11291/xxxx.xxxx.xxxx W/System.err:     at xxxx.xxxx.xxxx.Captureit.capturepicture(Captureit.java:72)
01-03 11:01:27.570 11291-11291/xxxx.xxxx.xxxx W/System.err:     at xxxx.xxxx.xxxx.StartingActivity$2.onClick(StartingActivity.java:48)
01-03 11:01:27.570 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.view.View.performClick(View.java:4463)
01-03 11:01:27.570 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.view.View$PerformClick.run(View.java:18770)
01-03 11:01:27.571 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.os.Handler.handleCallback(Handler.java:808)
01-03 11:01:27.571 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:103)
01-03 11:01:27.571 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.os.Looper.loop(Looper.java:193)
01-03 11:01:27.571 11291-11291/xxxx.xxxx.xxxx W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5292)
01-03 11:01:27.571 11291-11291/xxxx.xxxx.xxxx W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 11:01:27.571 11291-11291/xxxx.xxxx.xxxx W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
01-03 11:01:27.572 11291-11291/xxxx.xxxx.xxxx W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
01-03 11:01:27.572 11291-11291/xxxx.xxxx.xxxx W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
01-03 11:01:27.572 11291-11291/xxxx.xxxx.xxxx W/System.err:     at dalvik.system.NativeStart.main(Native Method)
01-03 11:01:27.572 11291-11291/xxxx.xxxx.xxxx W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime: FATAL EXCEPTION: main
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime: Process: xxxx.xxxx.xxxx, PID: 11291
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime: java.lang.NullPointerException
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3457)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3418)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at xxxx.xxxx.xxxx.Captureit.capturepicture(Captureit.java:72)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at xxxx.xxxx.xxxx.StartingActivity$2.onClick(StartingActivity.java:48)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.view.View.performClick(View.java:4463)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18770)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:808)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:103)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5292)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
01-03 11:01:27.574 11291-11291/xxxx.xxxx.xxxx E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

Please help me, I am stuck at it for hours. 请帮助我,我坚持了几个小时。 Note that, moving the code of Captureit.java to startingactivity.java makes the app works but the code becomes too messy. 请注意,将Captureit.java的代码移动到startingactivity.java会使应用程序正常工作,但是代码变得太乱了。 I just want to make them work in separate classes. 我只想让它们在单独的类中工作。

Your second file - captureit.java - cannot be an Activity. 您的第二个文件captureit.java不能是活动。 An Activity can only be created by calling startActivity() or startActivityForResult() from another activity with an intent. 只能通过从另一个有意图的活动中调用startActivity()startActivityForResult()来创建活动。 When you call new CaptureIt() the onCreate() method and other activity-specific methods are not called. 调用new CaptureIt()时,不会调用onCreate()方法和其他特定于活动的方法。 This means that the activity doesn't have the correct information to do some things - like starting another activity. 这意味着该活动没有正确的信息来执行某些操作-例如开始另一个活动。

Basically you're trying to start the camera activity from an activity that doesn't exist to Android, so it's throwing a NullPointerException. 基本上,您尝试从Android不存在的活动开始摄像头活动,因此会引发NullPointerException。

I'd recommend moving it back into the same file until you understand how moving between activities works in Android. 建议您将其移回同一文件中,直到您了解Android之间在活动之间进行移动的方式为止。

You could still split the helper methods ( getOutputMediaFile() , etc) into another file to keep the main activity file simple. 您仍然可以将辅助方法( getOutputMediaFile()等)拆分为另一个文件,以使主活动文件保持简单。 But don't make it an activity and don't call startActivityForResult from it. 但是不要将其startActivityForResult活动, 也不要从中调用startActivityForResult

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用Android Studio的意图的情况下从相机拍照 - How to take a picture from camera without using intent in android studio java 无法接受用户输入的 2 个连续字符串 - java couldn't take 2 continuous String input from user 无法通过JWS应用连接到MySQL数据库 - Couldn't connect to a MySQL DB via JWS app 三星Galaxy Tab 7.0从相机意图返回时重新启动应用程序 - Samsung Galaxy Tab 7.0 restarts app on return from camera intent 默认相机在android中拍照 - default camera to take a picture in android 如何使用 Android 应用程序中的 Intent 在专业模式下打开默认相机应用程序? - How to open the default camera app in professional mode using an intent in your android app? Android camera2:捕获请求键不包含我可以通过默认应用控制的设置 - Android camera2: capture request keys don't include settings I can control from the default app 无法通过AssetManager从zip存档中加载TextureAtlas-libgdx - Couldn't load TextureAtlas from zip archive via AssetManager - libgdx 通过意图将数据从应用程序传递到小部件时,空指针 - Null Pointer When Passing Data from App to Widget via Intent 通过Intent从Android应用程序以编程方式在Facebook中共享图像 - Share image in Facebook programmatically from Android app via Intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM