在Android中拍照后未调用onActivityResult [英] onActivityResult not called after taking a photo in Android

查看:433
本文介绍了在Android中拍照后未调用onActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码,但从未调用过我的onActivityResult.我过去发出请求时没有传递将图像保存到SD卡的额外意图,而且效果很好-onActivityResult()会被调用,正如我期望的那样.但是,因为我添加了SD卡代码,所以不走运!

I am using this code but my onActivityResult never gets called. I used to make the request without passing the extra intent to save the image to an SD card and that worked fine - onActivityResult() would get called as I expect it to. But since I added the SD card code - no luck!

我添加(或错过)了什么吗?我以 https://stackoverflow.com/a/12277455/2884981 为例.

Have I added (or missed) something ? I was following https://stackoverflow.com/a/12277455/2884981 as an example.

这是我的代码,

    static final int CAPTURE_IMAGE_CALLBACK = 1;

    private void dispatchTakePictureIntent() 
    {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File photo = null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
        {
            photo = new File(android.os.Environment.getExternalStorageDirectory(), "myapp/images/" + File.separator + timeStamp + ".png");
        } 
        else 
        {   
            photo = new File(getCacheDir(), "myapp/images/" + File.separator + timeStamp + ".png");
        }  
        if ( photo != null)
        {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        }

        if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
        {
            startActivityForResult(takePictureIntent, CAPTURE_IMAGE_CALLBACK);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        // This never gets hit!
        if (requestCode == CAPTURE_IMAGE_CALLBACK) 
        {
              // etc

我只是想添加一下,一旦单击勾号,便会在相机应用程序中添加-没有任何反应.我反复单击刻度,它一直停留在相机屏幕上.控件永远不会返回到主应用程序.

I just thought I'd add, inside the camera app once I click the tick - nothing happens. I click the tick repeatedly, it just stays on the camera screen. Control never gets returned to the main app.

推荐答案

我需要添加以下内容:

    photo.getParentFile().mkdirs();
    photo.createNewFile();

我相信失败的原因是因为我试图写入图像的文件不存在.

I believe the reason it was failing was because the file I was trying to write the image to didn't exist.

这篇关于在Android中拍照后未调用onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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