簡體   English   中英

如何在onActivityResult中調用startActivityForResult?

[英]How to call startActivityForResult inside onActivityResult?

調用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");
            }
        }

我已通過刪除photoPickerIntent.putExtra("crop","true");解決了此錯誤photoPickerIntent.putExtra("crop","true");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM