0

こんにちは、以下のコードの後、メソッド onActivityResult への呼び出しを取得できません。

private void ImageChooseOptionDialog() {

    Log.i(TAG, "Inside ImageChooseOptionDialog");
    String[] photooptionarray = new String[] { "Take a photo",
            "Choose Existing Photo" };

    Builder alertDialog = new AlertDialog.Builder(TabSample.tabcontext)
            .setItems(photooptionarray,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            if (which == 0) {
                                Log.i(TAG, "Which" + which);
                                Log.i(TAG,
                                        "Inside ImageChooseOptionDialog Camera");
                                _path = Environment
                                        .getExternalStorageDirectory()
                                        + File.separator
                                        + "TakenFromCamera.png";

                                Log.d(TAG, "----- path ----- " + _path);
                                media = _path;
                                // File file = new File(_path);
                                // Uri outputFileUri = Uri.fromFile(file);
                                // Intent intent = new Intent(
                                // android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                                // intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                // outputFileUri);
                                // startActivityForResult(intent, 1212);

                            } else if (which == 1) {
                                Log.i(TAG, "Which" + which);
                                Log.i(TAG,
                                        "Inside ImageChooseOptionDialog Gallary");
                                // Intent intent = new Intent();
                                // intent.setType("image/*");
                                // intent.setAction(Intent.ACTION_GET_CONTENT);
                                // startActivityForResult(Intent
                                // .createChooser(intent,
                                // "Select Picture"), 1);

                                Intent intent = new Intent(
                                        Intent.ACTION_GET_CONTENT);
                                intent.setType("image/*");
                                // intent.setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(Intent
                                        .createChooser(intent,
                                                "Select Picture"), 1);

                                Log.i(TAG, "end" + which);

                            }
                        }
                    });
    alertDialog.setNeutralButton("Cancel",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    alertDialog.create().show();
}

これは私の onActivityResult メソッドです:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "Inside Onactivity Result");
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Log.i(TAG, "Inside if else Onactivity Result");
            // currImageURI is the global variable I’m using to hold the
            // content:// URI of the image
            Uri currImageURI = data.getData();
            String path = getRealPathFromURI(currImageURI);
            Log.i(TAG, "Inside Onactivity Result Image path" + path);

        }
    }
}

私が間違っているところを教えてください。ダイアログボックスが表示された後、onActivityResult メソッドを which=1 から呼び出しています。しかし、logcat の onActivityResult メソッド内でログを取得できません。

4

1 に答える 1

1

これは、RESULT_OK が得られない可能性があるためです。常に最初にリクエスト コードをチェックし、その中で結果コードをチェックします。ギャラリーから画像を取得する場合は、onActivityResult() 内で次のコードを試してください。

if (requestCode == TAKE_PICTURE_GALLERY) {
            if (resultCode == RESULT_OK) {

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

                final Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                final int columnIndex = cursor
                        .getColumnIndex(filePathColumn[0]);
                final String filePath = cursor.getString(columnIndex);
                cursor.close();
             }
}

そして、必要な場所で filePath を使用します。これで問題が解決することを願っています。ありがとうございました :)

更新: このコードを使用して、ギャラリー アクティビティを開始します。

 imagePathURI = Uri.fromFile(new File(<your image path>));
        final Intent intent = new Intent(Intent.ACTION_PICK, imagePathURI);
                intent.setType("image/*");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePathURI);
                startActivityForResult(intent, TAKE_PICTURE_GALLERY);

ギャラリーから画像を取得する場合、MediaStore.EXTRA_OUTPUT は、要求された画像またはビデオを格納するために使用されるコンテンツ リゾルバー Uri を示すために使用されるインテント エクストラの名前を参照します。ここでは、画像を受け取る画像パスを渡す必要があります。 imagePathURI = Uri.fromFile(new File(<your image path>));// ここで、イメージ パスからファイルが作成されます。画像を受け取ったら、この画像パスから画像にアクセスできます。

于 2012-06-04T07:47:26.707 に答える