简体   繁体   English

API 22按意图发布的摄像头

[英]API 22 Camera by Intent Issue

I'm developing an android app currently with the min sdk being 19, and trying to support all the way up till 26. This week I've added a camera feature to an activity and I'm finding it ridiculously challenging to debug this. 我目前正在开发一个Android应用,最小sdk为19,并一直支持到26。本周,我在一项活动中添加了摄像头功能,发现调试它非常荒谬。

Basically the user clicks the camera icon, and it starts the android camera through an intent, user takes the picture, it gets saved, then added to the slider gallery they were viewing before launching the camera intent. 基本上,用户单击相机图标,然后通过一个意图启动android相机,用户拍照,将其保存,然后将其添加到他们正在查看的滑块库中,然后再启动相机意图。

So I finally got it working on API 19, 21, 23, 24, 25 and 26... The only API that is giving me an issue is 22 and I really cannot figure out why. 因此,我终于在API 19、21、23、24、25和26上使用了它。给我一个问题的唯一API是22,我真的不知道为什么。 When I click the camera icon, it launches the intent, the camera loads to a white screen, and the app crashes. 当我单击相机图标时,它会启动意图,相机加载到白屏,并且应用程序崩溃。 The kicker is this, I just tried it and it worked, thats twice now that the app successfully took the picture, saved it and did not crash. 更重要的是,我刚刚尝试了一下,它起作用了,这就是应用程序成功拍摄,保存并没有崩溃的两倍。

After it worked, i tried it again and I got this: 它起作用后,我再次尝试了,得到了这个:

09-09 17:19:22.381 6163-6163/com.android.camera E/Camera: Error 100 09-09 17:19:22.381 6163-6163/com.android.camera E/CameraErrorCallback: Got >camera error callback. 09-09 17:19:22.381 6163-6163 / com.android.camera E / Camera:Error 100 09-09 17:19:22.381 6163-6163 / com.android.camera E / CameraErrorCallback:得到了>相机错误回调。 error=100 09-09 17:19:22.381 6163-6163/com.android.camera E/AndroidRuntime: FATAL >EXCEPTION: main Process: >com.android.camera, PID: 6163 错误= 100 09-09 17:19:22.381 6163-6163 / com.android.camera E / AndroidRuntime:致命>异常:主进程:> com.android.camera,PID:6163

java.lang.RuntimeException: Media server died. java.lang.RuntimeException:媒体服务器死亡。 at >com.android.camera.CameraErrorCallback.onError(CameraErrorCallback.java:31) at >android.hardware.Camera$EventHandler.handleMessage(Camera.java:1148) at >android.os.Handler.dispatchMessage(Handler.java:102) at >android.os.Looper.loop(Looper.java:135) at >android.app.ActivityThread.main(ActivityThread.java:5254) at >java.lang.reflect.Method.invoke(Native Method) at >java.lang.reflect.Method.invoke(Method.java:372) at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at >com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 在> com.android.camera.CameraErrorCallback.onError(CameraErrorCallback.java:31)在> android.hardware.Camera $ EventHandler.handleMessage(Camera.java:1148)在> android.os.Handler.dispatchMessage(Handler.java: 102)在> android.os.Looper.loop(Looper.java:135)在> android.app.ActivityThread.main(ActivityThread.java:5254)在> java.lang.reflect.Method.invoke(本机方法)在> com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)处的java.lang.reflect.Method.invoke(Method.java:372)> com.android.internal.os.ZygoteInit 。主要(ZygoteInit.java:698)

And after trying it a bit later I got yet again, a different error.. 稍后尝试后,我又得到了另一个错误。

09-09 17:32:22.185 1530-1611/system_process E/AudioService: Media server died. 09-09 17:32:22.185 1530-1611 / system_process E / AudioService:媒体服务器死了。 09-09 17:32:22.191 4072-4072/com.android.camera E/AndroidRuntime: FATAL >EXCEPTION: main Process: >com.android.camera, PID: 4072 09-09 17:32:22.191 4072-4072 / com.android.camera E / AndroidRuntime:FATAL> EXCEPTION:main process:> com.android.camera,PID:4072

java.lang.RuntimeException: getParameters failed (empty parameters) at >android.hardware.Camera.native_getParameters(Native Method) at >android.hardware.Camera.getParameters(Camera.java:1888) at >com.android.camera.Camera.initializeZoom(Camera.java:489) at >com.android.camera.Camera.initializeFirstTime(Camera.java:392) at >com.android.camera.Camera.access$600(Camera.java:87) at >com.android.camera.Camera$MainHandler.handleMessage(Camera.java:290) at >android.os.Handler.dispatchMessage(Handler.java:102) at >android.os.Looper.loop(Looper.java:135) at >android.app.ActivityThread.main(ActivityThread.java:5254) at >java.lang.reflect.Method.invoke(Native Method) at >java.lang.reflect.Method.invoke(Method.java:372) at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at >com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) java.lang.RuntimeException:在> com.android.camera.Camera >> android.hardware.Camera.native_getParameters(Native Method)在> android.hardware.Camera.getParameters(Camera.java:1888)处的getParameters失败(空参数) .com.android.camera.Camera.initializeZoom(Camera.java:489)在> com.android.cam.camera.Camera.access $ 600(Camera.java:87)在> com.android.camera.Camera.initializeFirstTime(Camera.java:392)在.com。 android.os.Looper.loop(Looper.java:135)处的android.camera.Camera $ MainHandler.handleMessage(Camera.java:290)在> android.os.Handler.dispatchMessage(Handler.java:102)在> android.app.ActivityThread.main(ActivityThread.java:5254)位于> java.lang.reflect.Method.invoke(本地方法)位于> java.lang.reflect.Method.invoke(Method.java:372) .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903),位于> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Here is my code revolving around calling the camera function/activity: 这是围绕调用相机功能/活动的代码:

Setting camera icon onclick listener: 设置相机图标onclick侦听器:

public boolean onCreateOptionsMenu(Menu menu) {

        item.getActionView().setOnClickListener(new View.OnClickListener() >
            @Override
            public void onClick(View v) {
                startActivityForResult(CameraUtility.dispatchTakePictureIntent(getApplicationContext(), mushroomFolder), 1);
                return;
            }
        });
  return super.onCreateOptionsMenu(menu);
}

The on activity for result just adds the last taken photo to the gallery and is not even reached so I wont bother including it. 为获得结果而进行的活动仅将最后拍摄的照片添加到图库中,甚至还没有到达,因此我不会理会它。

And heres the code for my CameraUtility: 这是我的CameraUtility的代码:

class CameraUtility extends AppCompatActivity {
private static String CURRENT_PHOTO_PATH;
public static String PHOTO_DIRECTORY;

public static void setPhotoDirectory(Context c) {
    PHOTO_DIRECTORY = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/MyTrackerPhotos/";
}

public static Intent dispatchTakePictureIntent(Context c, String name) {
    String timeStamp = new SimpleDateFormat("dd-MM-yyyy_HHmmss").format(new Date());
    String path = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/MyTrackerPhotos/" + name + "/" + name + timeStamp + ".jpg";
    File file = new File(path);
    Uri outputFileUri;
    if (Build.VERSION.SDK_INT >= 23)           
        outputFileUri=FileProvider.getUriForFile(c,
        "com.tracker.mushroom.fileprovider", file);
    else
        outputFileUri = Uri.fromFile(file);
    CURRENT_PHOTO_PATH = file.getPath();
    Intent intent = new Intent(
            MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    return intent;
}

public static String getCurrentPhotoPath() {
    return CURRENT_PHOTO_PATH;
}

public static File[] getImageFiles(Context c, String mushroomFolder) throws IOException {
    String filePath = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/MyTrackerPhotos/" + mushroomFolder;
    File file = new File(filePath);
    file.mkdirs();
    file.setReadable(true);
    file.setWritable(true);
    return file.listFiles();
    }
}

So I'm not really sure if its an issue of my code, or if its the emulator or if its something else I don't even know about, because if every other API is working, I cant help but just scratch my head. 因此,我不太确定这是否是我的代码问题,模拟器还是其他我什至不知道的问题,因为如果其他所有API都在起作用,我会忍不住摸索。 I know the getParameters() error is related to the Camera class, but I am not even using it so I'm not sure why it even triggers that error. 我知道getParameters()错误与Camera类有关,但是我什至没有使用它,所以我不确定为什么它会触发该错误。 All and any input is welcome and much appreciated. 欢迎所有意见,并表示赞赏。

Well, I did some further testing and I think I am at the point where I am going to blame this on the emulator/android studio. 好吧,我进行了一些进一步的测试,我认为这是我将其归咎于模拟器/ Android Studio的时候。 I ran 3 firebase robo tests, all on api 22 and different phone models. 我在api 22和不同手机型号上进行了3次Firebase robo测试。

Each one of these continually opened and closed the camera activity just fine, unfortunately the tests never actually took a picture, but the crash was happening before that anyway. 这些功能中的每一个都可以正常打开和关闭相机活动,但是不幸的是,这些测试从未真正拍摄过照片,但是无论如何,崩溃都是在那之前发生的。

If I could test this out my self on a physical device that has API 22, and it works, I'd know for sure its an emulator/android issue. 如果我可以在具有API 22的物理设备上进行自我测试,并且可以正常工作,那么我肯定会知道它是模拟器/ android问题。 At this point I don't even feel its worth investing time to try and debug this especially after the robo tests. 在这一点上,我什至不觉得值得花时间尝试和调试它,尤其是在自动测试之后。 Hope someone finds this useful and saves some time chasing after some mutating ghost error, lol. 希望有人发现它有用,并在发生一些突变的虚假错误后大胆地节省时间。

EDIT: Would like to add as a final note.. I downloaded api 22 x86, installed it and made a new emulator. 编辑:想要添加为最后说明。我下载了api 22 x86,安装了它,并制作了一个新的模拟器。 No more problems. 没有更多的问题。 I was using api 22 x86_64, and for some reason it just does not like to work on that version. 我使用的是api 22 x86_64,由于某种原因,它只是不喜欢在该版本上工作。 Cheers to those who commented, you guys were pretty bang on. 欢呼那些发表评论的人,你们都很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM