2012-06-04 90 views
0

嗨,我不能在下面的代码后调用方法onActivityResult。无法调用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方法从对话框出现后= 1。 但在logcat中没有获取任何内部onActivityResult方法的日志。

+0

也许是因为你正在用createChooser而不是直接开始活动? –

回答

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

而且无论你想使用的文件路径。 我希望这可以解决您的问题。谢谢:)

UPDATE: 使用此代码,开始您的画廊活动:

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指Intent-的名称额外用于指示内容解析器Uri用于存储请求的图像或视频。所以在这里,你必须通过图像路径,你会收到你的图像。 imagePathURI = Uri.fromFile(new File(<your image path>)); //这里将从您的图像路径创建一个文件。当你收到图像时,你可以从这个图像路径访问图像。

+0

Thanx Shrikant。但正如你可以看到里面onActivityResult方法我首先把一个日志为“内部活动结果”!! ....但我cnt也得到这个登录输出。所以我认为该方法不会随时由运行时调用。正如你所说我会得到“内Onactivity结果”的日志,但我没有得到日志。 –

+0

请看我更新的答案。 – Shrikant

+0

Thnx为您的回应,但<您的图像路径>? meanse?你正在谈论哪条路?我没有得到。我想从gallary获得图像的路径。 –

相关问题