代码之家  ›  专栏  ›  技术社区  ›  Rohit Malish

如何在onActivityResult内部调用startActivityForResult?

  •  2
  • Rohit Malish  · 技术社区  · 13 年前

    使命感 startActivityForResult 给我 ActivityNotFoundException .我想知道打电话是可能的吗 启动ActivityForResult 在…内 onActivityResult ?如果是,那么我的代码可能有什么问题:

    这是代码:

    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 回复  |  直到 7 年前
        1
  •  0
  •   double-beep hudson solomon    7 年前

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