我正在尝试创建一个按钮,用于打开相机并拍照,然后重命名该图片。我搜索了代码,发现了一个,但是当我回想onClick方法时,应用程序崩溃了。
摄像机代码
// Capture Image code
Button savePicture = findViewById(R.id.act_edit_capture_image);
savePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//camera stuff
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("ddMMyy_HHmmss").format(new Date());
//folder stuff
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + " Soleya");
File image = new File(dir, "Sol_" + actId + "_" + timeStamp + ".jpg");
Uri imageUri = FileProvider.getUriForFile(
EditActivity.this,
"com.example.mustafa.progressupdate.provider", //(use your app signature + ".provider" )
image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri );
startActivityForResult(imageIntent, REQUEST_TAKE_PHOTO);
}
});
崩溃日志
--------- beginning of crash
09-12 23:46:22.387 23677-23677/com.example.mustafa.progressupdate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mustafa.progressupdate, PID: 23677
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera2/com.android.camera.CaptureActivity clip={text/uri-list U:content://com.example.mustafa.progressupdate.provider/external_files/Pictures%20Soleya/Sol_A22430_120918_234622.jpg} (has extras) } from ProcessRecord{ae97546 23677:com.example.mustafa.progressupdate/u0a94} (pid=23677, uid=10094) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1683)
at android.os.Parcel.readException(Parcel.java:1636)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3071)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:744)
at com.example.mustafa.progressupdate.EditActivity$7.onClick(EditActivity.java:303)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
清单权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
我什至发现这个app可以完成相同的任务,但是在运行它时……该应用程序也崩溃了
崩溃日志
--------- beginning of crash
09-12 23:50:32.410 24201-24201/com.example.nirvan.cameraexample3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nirvan.cameraexample3, PID: 24201
android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/20180912_235032.jpg exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8909)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8894)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at com.example.nirvan.cameraexample3.MainActivity$1.onClick(MainActivity.java:52)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
关于我所缺少或做错的任何帮助
答案 0 :(得分:1)
第一次崩溃-用户关闭了您的相机权限。或者可能是相机应用程序的相机许可。您可以通过运行checkPermission来检查您的权限,但是无法阻止它们(也不应该)。