永远不会调用onActivityResult

时间:2015-05-01 14:36:44

标签: java android android-intent onactivityresult

我想从画廊中挑选一张照片。我有一个隐含的意图和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();
        }
    }

感谢您的建议。

2 个答案:

答案 0 :(得分:0)

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

RESULT_LOAD_IMG = 1

如果在基本活动中设置了1 onaActivitResult,那么AddQuestionActivity onactivityResult将无法前进

答案 1 :(得分:-1)

更新您的代码:

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);
            }
        });
    }