首先感谢所有人,在来到这个问题之前,我已经搜索了很多,为此找到答案,有很多像我这样的问题,但我根本无法配置我需要的东西所以我希望我的问题不会得到重复,否则我会得到负面的声誉。所以我制作了一个手电筒应用程序,它仍然在一些使用Android 6.0及更高版本的设备上崩溃了7,这就是我所做的:
在onCreate我正在请求这样的权限:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if(checkSelfPermission(Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED){
startCamera();
}
else{
String [] permissionRequest = {Manifest.permission.CAMERA};
requestPermissions(permissionRequest,
CAMERA_PERMISSION_REQUEST_CODE);
}
}
else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N){
startCamera();
}
}
在on create方法之外,我已经覆盖了onRequestPermission:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
startCamera();
}else {
Toast.makeText(this,"App cannot start wihout granted permission",Toast.LENGTH_LONG).show();
}
}
}
这是我的startCamera()方法:
private void startCamera() {
flashLight = (ImageButton) findViewById(R.id.btnSwitch);
deviceHasFlash = getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
Button aboutBtn = (Button) findViewById(R.id.AboutBtn);
aboutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,PoupUp.class));
}
});
if (!deviceHasFlash) {
Toast.makeText(MainActivity.this, "Sorry, you device does not have a flash", Toast.LENGTH_LONG).show();
return;
} else {
////Here the app crashes on devices
camera = Camera.open();
parameter = camera.getParameters();
camera.startPreview();
hasCam = true;
}//////
flashLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isChecked = !isChecked;
if (isChecked)
flashLight.setImageResource(R.drawable.on);
else
flashLight.setImageResource(R.drawable.off);
turnOnOff(isChecked);
}
});
SeekBar sbar = (SeekBar) findViewById(R.id.seekBar2);
sbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
freq = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
同样在AndroidManifest中我声明了权限:
<uses-permission android:name="android.permission.CAMERA"/>;
<uses-feature android:name="android.hardware.camera"/>;
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera.flash" />
你们可以请帮助我在运行时为Android 6.0及更高版本实现权限请求,使用Toast消息,就像现在一样,我已经尝试过模拟器,这是许可问题Emulator photo,但我的应用程序不拍照和录制任何东西,因为它是一个手电筒应用程序,我想改变那个烤面包消息,但在一些物理设备仍然崩溃,我不得不提到我已经尝试使用PermissionDispatcher但我无法配置,这就是为什么我要求你的帮助:)。
当我按下概述按钮时,应用程序崩溃了(我认为它是如何被调用的,主页按钮旁边的那个),这里是它的图片(Button)你能帮助我吗?有了这个?因为我不知道如何调用它,我无法找到它的修复:)
许可的错误日志:
Fatal Exception: java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bucur.mihai.flash/com.bucur.mihai.flash.MainActivity}:
java.lang.RuntimeException: Fail to connect to camera service
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:525)
at android.hardware.Camera.open(Camera.java:381)
at com.bucur.mihai.flash.MainActivity.startCamera(MainActivity.java:110)
at com.bucur.mihai.flash.MainActivity.onCreate(MainActivity.java:70)
at android.app.Activity.performCreate(Activity.java:6034)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1020 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
按钮按错误的错误日志:
Fatal Exception: java.lang.RuntimeException: Failed to resolve attribute at index 0
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:616)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6786)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6955)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:679)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.support.v7.view.menu.ListMenuPresenter.getMenuView(ListMenuPresenter.java:105)
at android.support.v7.app.AppCompatDelegateImplV9$PanelFeatureState.getListMenuView(AppCompatDelegateImplV9.java:2030)
at android.support.v7.app.AppCompatDelegateImplV9.initializePanelContent(AppCompatDelegateImplV9.java:1310)
at android.support.v7.app.AppCompatDelegateImplV9.openPanel(AppCompatDelegateImplV9.java:1152)
at android.support.v7.app.AppCompatDelegateImplV9.onKeyUpPanel(AppCompatDelegateImplV9.java:1517)
at android.support.v7.app.AppCompatDelegateImplV9.onKeyUp(AppCompatDelegateImplV9.java:964)
at android.support.v7.app.AppCompatDelegateImplV9.dispatchKeyEvent(AppCompatDelegateImplV9.java:958)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:318)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2438)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4582)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4537)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4201)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4258)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4234)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4421)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2480)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2074)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2065)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2457)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)