Android摄像头活动不回我的应用程序时确定按钮是pressed [英] Android camera activity doesn't return to my app when the OK button is pressed

查看:160
本文介绍了Android摄像头活动不回我的应用程序时确定按钮是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序的完全合作。它只是1的问题,我的相机意图或权限。

I have an app that's fully working. It just has 1 problem with my Camera Intent or permissions.

该应用程序应该启动相机的活动,当用户presses一个按钮。这是工作正常,但是当用户接受图片点击相机上的OK按钮,它不回来我的应用程序。如果preSS相机上的取消按钮,它回来给我的应用程序如预期。

The app is supposed to launch the Camera Activity when the user presses a button. That's working fine, but when the user accepts a picture by clicking the OK button on the camera, it doesn't come back to my app. If they press the cancel button on the camera, it comes back to my app as expected.

我看了所有的类似的问题我在这里可以找到,但他们都没有固定我的问题。我要告诉相机究竟在何处保存图像,因为我想在我的应用程序2之间交替。这里的code,创建相机意向,并开始吧:

I have read all the similar questions I can find on here, but none of them have fixed my problem. I need to tell the camera exactly where to save the image because I want to alternate between 2 in my app. Here's the code that creates the Camera Intent and starts it:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.parse(imagePath));
startActivityForResult(intent, 11);

我已经试过了的ImagePath (这是一个字符串)多个值,但相机的OK键并没有与任何人合作。这是我试过的路径:

I have tried multiple values for imagePath (which is a String), but the camera's OK button has not worked with any of them. These are the paths I've tried:

/mnt/sdcard/<appName>/cameraImages/image1.jpg
from Environment.getExternalStorageDirectory()

/mnt/sdcard/Android/data/<appPkg>/cache/<appName>/cameraImages/image1.jpg
from context.getExternalCacheDir()

/mnt/sdcard/Android/data/<appPkg>/cache/<appName>/cameraImages/image1.jpg
from context.getCacheDir()

我在清单文件中的以下权限:

I have the following permissions in my manifest file:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

你们可以帮我找出什么地方错了我的code,或者告诉我什么是有效的路径可能是什么?我preFER如果图像存储在缓存中的位置,因为它不是重要的是形象,继续现有的用户离开该应用程序后,但是我真的不关心谁有权访问图像。

Can you guys help me figure out what's wrong with my code, or tell me what a valid path could be? I prefer if the image is stored in a cache location because it's not important for the image to continue existing after the user leaves the application, but I don't really care who has access to the image.

我做的4.1模拟器所有的测试,以4.1 SDK编译和我的最低版本的API 8(2.2)。

I'm doing all testing on the 4.1 emulator, compiling with the 4.1 SDK, and my minimum version is API 8 (2.2).

我已经把为目的的情况下在 onActivityResult(),但我不认为你们需要看到,code,因为它不应该影响问题。

I have put a case for the intent in onActivityResult(), but I don't think you guys need to see that code because it shouldn't be affecting the problem.

推荐答案

我能弄清楚这个问题。看来,而不是使用 Uri.parse(的ImagePath),你必须使用 Uri.fromFile(新文件(的ImagePath))当你添加额外的。我已经把新的意图code以下的情况下,它可以帮助任何人以同样的问题。

I was able to figure out the issue. It seems that instead of using Uri.parse(imagePath), you have to use Uri.fromFile(new File(imagePath)) when you add the extra. I've put the new Intent code below in case it helps anyone with the same problem.

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(imagePath)));
startActivityForResult(intent, 11);

我现在已经成功地使用 context.getExternalCacheDir()以创建完整路径的ImagePath 。我创建的结果路径<$c$c>/mnt/sdcard/Android/data/<appPkg>/cache/<appName>/cameraImages/image1.jpg.

I am now successfully using context.getExternalCacheDir() to create the full path imagePath. The resulting path that I created is /mnt/sdcard/Android/data/<appPkg>/cache/<appName>/cameraImages/image1.jpg.

这篇关于Android摄像头活动不回我的应用程序时确定按钮是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆