Android Studio App崩溃,与OnClick和startActivityForResult相关

时间:2016-06-25 04:24:18

标签: java xml android-studio camera

我正在学习如何在Android Studio上制作一个简单的相机应用程序。当我点击相机'按钮,应用程序崩溃。我似乎无法确定导致应用崩溃的原因。在logcat中,它声明了' startActivityForResult'可能是原因...但对我来说看起来很正常。

我确保相机按钮ID与java文件匹配。非常感谢任何帮助!

到目前为止,这是我的代码。

Main_Activity.java:

public class MainActivity extends AppCompatActivity {

Button cam_button;
ImageView imageView;
static final int CAMERA_REQUEST_CODE = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    cam_button = (Button) findViewById(R.id.cam_button);
    imageView = (ImageView) findViewById(R.id.imageV);

    cam_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = getFile();
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
        }
    });

}


private File getFile() {
    File folder = new File("sdcard/camera_app");
    if(folder.exists())
        folder.mkdir();

    File imageFile = new File("cameraImage.jpg");

    return imageFile;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    String path = "sdcard/camera_app/cameraImage.jpg";
    imageView.setImageDrawable(Drawable.createFromPath(path));
}}

activity_main.xml中:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="camera"
    android:id="@+id/cam_button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageV"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

这是我从logcat中复制的内容:

  

06-24 21:30:45.769 21481-21481 / com.sugarcoder.cameraapp   E / AndroidRuntime:致命异常:主要                                                                         处理:com.sugarcoder.cameraapp,PID:21481                                                                         java.lang.SecurityException:Permission Denial:启动Intent {   act = android.media.action.IMAGE_CAPTURE flg = 0x3   cmp = com.motorola.camera / .Camera clip = {text / uri-list   U:file:///cameraImage.jpg}(有额外内容)}来自ProcessRecord {b5b85f2   21481:com.sugarcoder.cameraapp / u0a121}(pid = 21481,uid = 10121)   撤销权限android.permission.CAMERA                                                                             在android.os.Parcel.readException(Parcel.java:1620)                                                                             在android.os.Parcel.readException(Parcel.java:1573)                                                                             在   android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2696)                                                                             在   android.app.Instrumentation.execStartActivity(Instrumentation.java:1574)                                                                             在android.app.Activity.startActivityForResult(Activity.java:3921)                                                                             在   android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)                                                                             在   android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)                                                                             在android.app.Activity.startActivityForResult(Activity.java:3881)                                                                             在   android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)                                                                             在   com.sugarcoder.cameraapp.MainActivity $ 1.onClick(MainActivity.java:40)                                                                             在android.view.View.performClick(View.java:5201)                                                                             在android.view.View $ PerformClick.run(View.java:21163)                                                                             在android.os.Handler.handleCallback(Handler.java:746)                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                             在android.os.Looper.loop(Looper.java:148)                                                                             在android.app.ActivityThread.main(ActivityThread.java:5443)                                                                             at java.lang.reflect.Method.invoke(Native Method)                                                                             在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

1 个答案:

答案 0 :(得分:0)

解决!我发现问题是我必须真正进入手机上的应用程序的应用程序信息,并打开权限。伙计们,感谢您的帮助! :)