2012-06-04 88 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獲得圖像的路徑。 –

相關問題