android - 在我单击后退按钮之前,相机不会打开 - android

标签 android onclicklistener

在我的 Android 应用程序中,当我首先点击按钮时,它应该打开相机,一旦我捕捉到图像,它应该重定向到一个新的 Activity。

但实际情况是,当我首先点击按钮时,它会重定向到新 Activity ,一旦我点击后退按钮,它就会打开相机。

我不明白我的代码有什么问题,看起来一切正常。有什么帮助吗?

    public void onCameraButtonClicked()
    {

        camera_btn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
                        makingDirectory(dir);

                        storePicture(dir);
                    }
                }
        );
    }

    public void makingDirectory(String dir)
    {
        File newdir = new File(dir);
        newdir.mkdirs();
    }

    public void storePicture(String dir)
    {
        boolean flag_storePicture = false;
        // Here, the counter will be incremented each time, and the
        // picture taken by camera will be stored as 1.jpg,2.jpg
        // and likewise.
        count++;
        String file = dir+count+".jpg";
        File newfile = new File(file);
        try
        {
            newfile.createNewFile();
            Uri outputFileUri = Uri.fromFile(newfile);

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

            startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
            flag_storePicture = true;
            Log.d("myApp", "Hiiiiii from inside");
        }
        catch (IOException e)
        {

        }
        finally {
            Log.d("myApp", "Hiiiiii");
            Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera");
            startActivity(camera_intent);
        }
    }

在我的 logcat 中,我收到了这条消息。

Skipped 62 frames!  The application may be doing too much work on its main thread.

最佳答案

你也很清楚,问题是控制流,

在 try block 中,camera 应用已为您打开

 startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

然后紧接着 finally 被执行

finally {
            Log.d("myApp", "Hiiiiii");
            Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera");
            startActivity(camera_intent);
        }

现在您在相机应用程序上有了 googlemap1.Camera Activity ,因此如果您按回车键,您将找到您的相机应用程序。

解决方案:根据您的要求,将您的 finally block 代码连同 if 条件一起移动到 onActivityResult 以验证图像捕获任务完成。喜欢

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK){ // image captured successfully 
            //..code to start your other activity 
        Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera");
        startActivity(camera_intent);
        }else{
           // ..  image capture failure , user pressed cancel etc
           }
    }

关于android - 在我单击后退按钮之前,相机不会打开 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40310093/

相关文章:

android - 如何在android中自动调用我的应用程序

php - 如何将从数据库中检索到的某些行数据显示到 listView 中

android - 网络应用 : Hide a div except android browsers

java - 图像按钮设置背景 onClick

android - 如何替换 fragment 中textView的文本

android - 使用 socket.io 中的每个 socket.emit 获取多个套接字响应

java - 应为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处却是 BEGIN_OBJECT?

javascript - 删除 JavaScript 中的单击事件

android - 在 Android 按钮中实现滑动和单击

android - 以编程方式添加的按钮的不同点击