android中的相机为resultCode

时间:2015-12-07 06:40:10

标签: android android-camera

当我在android中使用resultCode捕获图像时,我得到Intent -1。

这是我的按钮点击代码。

   public void capture(View view)
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageFile = new File(
                Environment.
                        getExternalStoragePublicDirectory
                                (Environment.DIRECTORY_PICTURES) ,
                "Test"
                );
        Uri imageUri = Uri.fromFile(imageFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT , imageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY , 1);
        startActivityForResult(intent, 3);
    }

这是我的应用程序的ActivityResult方法.....

  protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == 3)
        {
            Log.i("Result code is : \t" , resultCode+"");
            switch (resultCode)
            {
                case Activity.RESULT_OK :
                    if (imageFile.exists())
                    {
                        Toast.makeText(this , "File saved at \n"+imageFile.getAbsolutePath() , Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(this , "Error while capturing" , Toast.LENGTH_SHORT).show();
                    }

                    break;

                case Activity.RESULT_CANCELED :

                    break;

                default :
                    Toast.makeText(this , "Some error" , Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

请帮帮我,我得到结果代码-1 .... 有人请帮帮我.... 我是android的新手......

1 个答案:

答案 0 :(得分:2)

您正在使用意图来调用活动并从活动中请求结果。

public void capture(View view)
{
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imageFile = new File(
            Environment.
                    getExternalStoragePublicDirectory
                            (Environment.DIRECTORY_PICTURES) ,
            "Test"
            );
    Uri imageUri = Uri.fromFile(imageFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT , imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY , 1);
    startActivityForResult(intent, 3);
}

并在OnAcitvityResult上使用

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == 3)
    {
        Log.i("Result code is : \t" , resultCode+"");
        switch (resultCode)
        {
            case RESULT_OK :
                if (imageFile.exists())
                {
                    Toast.makeText(this , "File saved at \n"+imageFile.getAbsolutePath() , Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(this , "Error while capturing" , Toast.LENGTH_SHORT).show();
                }

                break;

            case RESULT_CANCELED :

                break;

            default :
                Toast.makeText(this , "Some error" , Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

默认情况下,RESULT_OK和RESULT_CANCELLED的值在ACIVITY类中定义。

在Acivity Class

 /** Standard activity result: operation canceled. */
    public static final int RESULT_CANCELED    = 0;
    /** Standard activity result: operation succeeded. */
    public static final int RESULT_OK           = -1;
    /** Start of user-defined activity results. */
    public static final int RESULT_FIRST_USER   = 1;

这就是你得到RESULT_OK = -1;

的原因