簡體   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