某些android设备上的相机访问错误 [英] Camera access error on some android devices
问题描述
我写了一个使用手机相机的应用程序.在我尝试过的大多数设备上,它都能正常工作,但有些设备在打开时崩溃.进行一些调试后,发现尝试访问相机时出现问题.
I wrote an app which utlizes the phone's camera. On most of the devices I tried it worked fine but with some the application crashes on opening. A little debugging showed that there's a problem when trying to access the camera.
完整错误消息:
06-14 23:15:01.550 21872-21872/bguproject.vlc E/AndroidRuntime: FATAL EXCEPTION: main
Process: bguproject.vlc, PID: 21872
java.lang.RuntimeException: Unable to start activity ComponentInfo{bguproject.vlc/bguproject.vlc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
at bguproject.vlc.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
如您所见,错误表明我尝试在空对象引用上调用虚拟方法,如果正确完成mCamera的写操作将不会发生
as you can see the error says I try to invoke virtual method on a null object reference which wouldn't happen if the writing of mCamera was done properly
代码(内容写在结尾的try-catch短语上):
The code (the writing is on the try-catch phrase at the end):
package bguproject.vlc;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ScrollView;
public class MainActivity extends Activity implements View.OnClickListener{
private Camera mCamera = null;
private CameraView mCameraView = null;
private int fpsRate[] = new int[2];
private boolean work = true,receiving = false;
private boolean send = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Logger l = Logger.getInstance(this); //create the logger instance
Logger.Log("Starting VLC app...");
try{
bguproject.vlc.Logger.Log("\tAccessing camera...");
mCamera = Camera.open();//you can use open(int) to use different cameras
} catch (Exception e){
bguproject.vlc.Logger.LogError("\tFailed to get camera: " + e.getMessage());
}
完整代码: http://pastebin.com/hxqJK3t2
所有被压碎的手机都具有可以正常工作的相机,并且相对较新(三星s6,三星s7和LG g4).
all the phones that crushed had a working camera and were relatively new (Samsung s6, samsung s7 and LG g4).
推荐答案
三星S6和LG G4都可以升级到Android棉花糖(v6或API 23),而三星S7则在普通ROM上运行棉花糖,所以我假设所有这3种的设备正在运行棉花糖:)
Samsung S6 and LG G4 both are upgradable to Android Marshmallow (v6 or API 23) and Samsung S7 is running Marshmallow on stock ROM so I'll assume all 3 of that devices are running Marshmallow :)
来自 https://developer.android.com/training/permissions/requesting.html
注意:从Android 6.0(API级别23)开始,即使该应用程序定位于较低的API级别,用户也可以随时从任何应用程序撤消权限.无论您的应用程序所针对的API级别是什么,您都应该测试您的应用程序以验证其在缺少所需权限时是否能够正常运行.
From https://developer.android.com/training/permissions/requesting.html
Note: Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level. You should test your app to verify that it behaves properly when it's missing a needed permission, regardless of what API level your app targets.
除了清单中设置的权限外,您还需要在运行时请求/检查权限.您可以在其中使用示例代码,或者...
Aside from the permissions set in the manifest, you'll need to request/check for permission on runtime. There are sample codes in there you can use, or...
快速解决方案,
Quick solution,
转到设置"->应用程序"->(您的应用程序名称)"->权限",然后启用相机权限.完成,尽管不建议将其用于最终产品
go to Settings-> Apps->(Your app name)->Permissions and enable the camera permission. Done, although not recommended for final product
然后再次尝试您的应用.应该现在可以工作了:D
then try your app again. Should be working now :D
这篇关于某些android设备上的相机访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!