繁体   English   中英

永远不会调用onActivityResult

[英]onActivityResult is never called

我想从画廊挑选一张照片。 我有一个隐式的intent和onActivityResult,但从未调用过它。 我一直在寻找这样的可能的解决方案:

setResult(RESULT_LOAD_IMG, galleryIntent);

这是我在AddQuestionActivity中的代码:

    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;
    public static Intent galleryIntent;

imageView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                // Create intent to Open Image applications like Gallery, Google Photos
                galleryIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                // Set the Image in ImageView after decoding the String
                imageView.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }
    }

谢谢你的建议。

如果有,请在onActivityResult检查您的基本活动。

RESULT_LOAD_IMG = 1

如果基本活动onaActivitResult中设置为1 ,则AddQuestionActivity onactivityResult将不会继续进行

更新您的代码:

imageView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                // Create intent to Open Image applications like Gallery, Google Photos
                Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
            }
        });
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM