如何在 onActivityResult 中调用 startActivityForResult?

Posted

技术标签:

【中文标题】如何在 onActivityResult 中调用 startActivityForResult?【英文标题】:How to call startActivityForResult inside onActivityResult? 【发布时间】:2012-08-27 05:45:02 【问题描述】:

打电话给startActivityForResult 给我ActivityNotFoundException。我想知道是否可以在onActivityResult 中调用startActivityForResult?如果是,那么我的代码可能有什么问题:

代码如下:

OnPreferenceClickListener selectListener = new OnPreferenceClickListener() 

        public boolean onPreferenceClick(Preference preference) 
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            photoPickerIntent.putExtra("crop","true");
            startActivityForResult(photoPickerIntent, 123);
            return false;
        

    ;

    protected void onActivityResult(int requestCode, int resultCode, Intent resultIntent)  
        super.onActivityResult(requestCode, resultCode, resultIntent);

        if(requestCode == 123)
            if(resultCode == RESULT_OK)
                Uri selectedImage = resultIntent.getData(); // uri from user selection.
                File tempFile; // temporary File
                Uri tempUri; // temporary Uri

                try 
                    // initializing File
                    tempFile = File.createTempFile("crop", ".png", Environment.getExternalStorageDirectory());
                 catch (IOException e1) 
                    tempFile = null;
                

                if(tempFile != null)
                    // initializing Uri
                tempUri = Uri.fromFile(tempFile);   
                else
                    tempUri = selectedImage;
                

                Display display = getWindowManager().getDefaultDisplay(); 
                int width = display.getWidth();
                int height = display.getHeight();

                // creating new crop intent
                final Intent cropIntent = new Intent("com.android.camera.action.CROP");
                cropIntent.setData(selectedImage); // original image Uri
                cropIntent.putExtra("outputX", width);
                cropIntent.putExtra("outputY", height);
                cropIntent.putExtra("aspectX", width);
                cropIntent.putExtra("aspectY", height);
                cropIntent.putExtra("scale", true);
                cropIntent.putExtra("noFaceDetection", true);
                cropIntent.putExtra("output", tempUri);  // cropped image Uri
                cropIntent.putExtra("outputFormat", "png");
                startActivityForResult(cropIntent, 456);
            

        if(requestCode == 456)
            if(resultCode == RESULT_OK)
                System.out.println("inside request code 456");
            
        

【问题讨论】:

【参考方案1】:

我已经通过删除photoPickerIntent.putExtra("crop","true");解决了这个错误。

【讨论】:

以上是关于如何在 onActivityResult 中调用 startActivityForResult?的主要内容,如果未能解决你的问题,请参考以下文章

在 OnCreate() Android 中为联系人调用 onActivityResult

onActivityResult 方法调用按钮单击[重复]

片段 onActivityResult 方法执行调用活动 onActivityResult

OnActivityResult 没有被调用

onActivityResult 没有在活动之间调用

从片段捕获图像时从不调用onActivityResult [重复]