按下“确定”按钮后默认相机活动未完成
我从我的活动中调用默认相机,然后处理 onActivityResult。我的代码似乎在 LG Ally 上运行良好,拍照时没有确认。但是,当我在 Nexus S 上运行相同的应用程序时,它会在返回我的活动之前提示我“确定”、“重新获取”或“取消”。虽然“取消”有效,返回到我的活动而不保存图片,但“确定”似乎没有任何效果,甚至没有返回到我的活动。
我的代码如下:
private void captureImage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + (new UserContextAdapter(this)).getUser() + "/");
path.mkdirs();
File file = new File(path, "Image_Story_" + mRowId.toString() + ".jpg");
newImageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);
startActivityForResult(intent, CAPTURE_IMAGE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case CAPTURE_IMAGE:
switch (resultCode ) {
case 0:
Log.i("CAPTURE", "Cancelled by User");
break;
case -1:
mImageUri = newImageUri;
setImageFromUri();
}
}
I'm calling the default camera from my activity and then handling the onActivityResult. My code seems to work fine on the LG Ally which doesn't have a confirmation when a picture is taken. However, when I run the same app on the Nexus S, it prompts me with an "Ok", "Retake", or "Cancel" before returning to my activity. While "Cancel" works, returning to my activity without saving the picture, "Ok" doesn't seem to have any effect, not even returning to my activity.
My code below:
private void captureImage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + (new UserContextAdapter(this)).getUser() + "/");
path.mkdirs();
File file = new File(path, "Image_Story_" + mRowId.toString() + ".jpg");
newImageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);
startActivityForResult(intent, CAPTURE_IMAGE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case CAPTURE_IMAGE:
switch (resultCode ) {
case 0:
Log.i("CAPTURE", "Cancelled by User");
break;
case -1:
mImageUri = newImageUri;
setImageFromUri();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想我也遇到了完全相同的问题。
如果保存图片的路径不正确,相机将不会返回到您的应用程序。一旦我确保该目录存在,一切就正常了。确保该目录存在,然后它应该可以工作。
-- 编辑 --
我刚刚看到,您调用了
path.mkdirs();
但我认为它不起作用。正如您可以在 android 文档中读到的那样“请注意,此方法在失败时不会抛出 IOException。调用者必须检查返回值。”。请务必检查该目录是否确实存在。哈
I think I just had the exact same problem.
If the path to save the picture isn't correct, the camera won't return to your app. Once I made sure the directory exists, everything worked fine. Make sure the directory exists, then it should work.
-- Edit --
I just saw, that you call
path.mkdirs();
but I think that it doesn't work. As you can read in the android doc "Note that this method does not throw IOException on failure. Callers must check the return value.". Please be sure to check if the directory really exists.hth
另外,如果您使用
Environment.getExternalStorageDirectory().getPath(),请确保您的应用程序具有
上面。希望这有帮助 =)
Also, make sure your application has
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
if you're usingEnvironment.getExternalStorageDirectory().getPath()
above.Hope this helps =)
请检查此
案例 1:
案例 2:
在上述两种情况下,我都可以通过 Nexus 上的相机保存图像
对于情况1:
a.图像存储在自定义文件夹中。
b.如果“System.currentTimeMillis()”更改为(“new Date().toString()”),则图像不会保存,并且相机不会返回到我的活动。
(可能是因为“System.currentTimeMillis”没有空格,而“new Date().toString()”可能有一些特殊字符和空格)
情况2:
一个。图像存储在相机文件夹中
感谢大家
please check this
Case 1:
Case 2:
I am able to save images through camera on nexus s in both of the above cases
In case 1:
a.Image is stored in custom folder.
b. If the “System.currentTimeMillis()” is changed to (“new Date().toString()”) image is not saved and camera does not return to my activity.
(Probably because “System.currentTimeMillis” has no spaces and “new Date().toString()” might be having some special characters and spaces)
In case 2:
a. Image is stored in camera folder
Thanks to all
我有同样的问题。您只需做一项工作,在手机存储中检查您是否有 Pictures 目录。如果您没有这样的库,则手动创建它。
希望这对你有用。
I have same problem.You have to do only one job,in your Phone storage check that you have Pictures directory or not.If you don't have such library then make it manually.
Hope this works for you.