我正在编写一个按下按钮的应用程序,它必须打开相机。但我的Nexus 5
正在运行Android 6.01
,我的应用不断崩溃。
这是我的onCLick听众。
public void capturePhoto(View view) {
Log.d(TAG, "Capture button pressed.");
Intent cameraApplicationIntent = new Intent();
cameraApplicationIntent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
cameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraApplicationIntent, CAMERA_INTENT_REQUEST);
}
我在清单中有这些:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
这是我的堆栈跟踪,第161行是
cameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
:
01-24 01:42:50.859 26744-26744/kiam.cameraintentpractice D/ImageAcquisition﹕ Capture button pressed.
01-24 01:42:50.873 26744-26744/kiam.cameraintentpractice W/System.err﹕ java.io.IOException: open failed: EACCES (Permission denied)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at java.io.File.createNewFile(File.java:939)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at java.io.File.createTempFile(File.java:1004)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at kiam.cameraintentpractice.ImageAcquisition.createImageFile(ImageAcquisition.java:197)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at kiam.cameraintentpractice.ImageAcquisition.capturePhoto(ImageAcquisition.java:157)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at android.view.View.performClick(View.java:5204)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at android.view.View$PerformClick.run(View.java:21153)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at libcore.io.Posix.open(Native Method)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ at java.io.File.createNewFile(File.java:932)
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice W/System.err﹕ ... 14 more
01-24 01:42:50.874 26744-26744/kiam.cameraintentpractice D/AndroidRuntime﹕ Shutting down VM
01-24 01:42:50.876 26744-26744/kiam.cameraintentpractice E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kiam.cameraintentpractice, PID: 26744
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4458)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:452)
at kiam.cameraintentpractice.ImageAcquisition.capturePhoto(ImageAcquisition.java:161)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
在Android 6.0及更高版本中,仅仅拥有清单权限是不够的。您需要在运行时请求权限:)
文档: http://developer.android.com/training/permissions/requesting.html