按下“确定”按钮后默认相机活动未完成

发布于 2024-12-02 16:53:32 字数 1233 浏览 2 评论 0原文

我从我的活动中调用默认相机,然后处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳哥哥 2024-12-09 16:53:32

我想我也遇到了完全相同的问题。

如果保存图片的路径不正确,相机将不会返回到您的应用程序。一旦我确保该目录存在,一切就正常了。确保该目录存在,然后它应该可以工作。

-- 编辑 --

我刚刚看到,您调用了 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

套路撩心 2024-12-09 16:53:32

另外,如果您使用 Environment.getExternalStorageDirectory().getPath(),请确保您的应用程序具有 上面。

希望这有帮助 =)

Also, make sure your application has <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> if you're using Environment.getExternalStorageDirectory().getPath() above.

Hope this helps =)

十秒萌定你 2024-12-09 16:53:32

请检查此

案例 1:

Uri newImageUri = null;

File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/");

path.mkdirs();

boolean setWritable = false;

setWritable = path.setWritable(true, false);

File file = new File(path, "Image_Story_" + System.currentTimeMillis() + ".jpg");

newImageUri = Uri.fromFile(file);

Log.i("MainActivity", "new image uri to string is " + newImageUri.toString());

Log.i("MainActivity", "new image path is " + newImageUri.getPath());            

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

案例 2:

String fileName = "" + System.currentTimeMillis() + ".jpg";

ContentValues values = new ContentValues();

values.put(MediaStore.Images.Media.TITLE, fileName);

values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");

values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Log.i("MainActivity", "new image uri to string is " + imageUri.toString());

Log.i("MainActivity", "new image path is " + imageUri.getPath());

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

在上述两种情况下,我都可以通过 Nexus 上的相机保存图像
对于情况1:
a.图像存储在自定义文件夹中。
b.如果“System.currentTimeMillis()”更改为(“new Date().toString()”),则图像不会保存,并且相机不会返回到我的活动。
(可能是因为“System.currentTimeMillis”没有空格,而“new Date().toString()”可能有一些特殊字符和空格)
情况2:
一个。图像存储在相机文件夹中

感谢大家

please check this

Case 1:

Uri newImageUri = null;

File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/");

path.mkdirs();

boolean setWritable = false;

setWritable = path.setWritable(true, false);

File file = new File(path, "Image_Story_" + System.currentTimeMillis() + ".jpg");

newImageUri = Uri.fromFile(file);

Log.i("MainActivity", "new image uri to string is " + newImageUri.toString());

Log.i("MainActivity", "new image path is " + newImageUri.getPath());            

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

Case 2:

String fileName = "" + System.currentTimeMillis() + ".jpg";

ContentValues values = new ContentValues();

values.put(MediaStore.Images.Media.TITLE, fileName);

values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");

values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Log.i("MainActivity", "new image uri to string is " + imageUri.toString());

Log.i("MainActivity", "new image path is " + imageUri.getPath());

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

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

惜醉颜 2024-12-09 16:53:32

我有同样的问题。您只需做一项工作,在手机存储中检查您是否有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文