2017-09-15 67 views
0

我從我的片段開始了標準的Android相機應用程序。一切工作正常,圖像得到保存,因爲我希望。在用戶拍攝照片(並且只有這樣)之後,我想啓動一個DialogFragment。在Android中使用相機應用程序後打開DialogFragment

我嘗試使用intent並從同一個片段調用onActivetyResult()。但onActivityResult()永遠不會被調用。

我在片段如何啓動相機:

MyCamera camera = new MyCamera(getActivity()); 
    camera.start(); 

林我MyCamera類我開始像這樣的意圖:

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
activity.startActivityForResult(takePictureIntent, Constants.TAKE_PHOTO); 

回到我打電話onActivityResult的片段() :

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == Constants.TAKE_PHOTO && resultCode == Activity.RESULT_OK) { 
     // do something 
    } 
} 

我該如何完成這項任務?先謝謝您的幫助!

這裏整個MyCamera類:

public class MyCamera { 
    private Activity activity; 
    private static final String TAG = "MyCamera"; 
    private String currentPhotoPath; 
    private String currentFileName; 
    private File currentFile; 
    private Uri contentUri; 

    public MyCamera(Activity activity) { 
     this.activity = activity; 
    } 

    public void start() { 
     dispatchPictureIntent(); 
     addPictureToGallery(); 
     currentPhotoPath = null; 
    } 

    private void dispatchPictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     // check if device has camera 
     if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) { 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
       currentPhotoPath = photoFile.getAbsolutePath(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
       Toast.makeText(activity, R.string.msg_picture_not_saved, Toast.LENGTH_LONG).show(); 
      } 
      if (photoFile != null) { 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
       takePictureIntent.putExtra(Constants.IMAGE_FILE_NAME, currentFileName); 
       activity.startActivityForResult(takePictureIntent, Constants.TAKE_PHOTO); 
      } 
     } 
    } 

    private File createImageFile() throws IOException { 
     // set up storage dir 
     File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
     if (!storageDir.exists()) { 
      if (!storageDir.mkdirs()) { 
       Log.e(TAG, "failed to create directory"); 
       return null; 
      } 
     } 
     // set up file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String fileName = "img_" + timeStamp + "_"; 
     String suffix = ".jpg"; 

     File image = File.createTempFile(fileName, suffix, storageDir); 
     currentFileName = image.getName(); 
     currentFile = image; 
     return image; 
    } 

    private void addPictureToGallery() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     contentUri = getUri(currentPhotoPath); 
     mediaScanIntent.setData(contentUri); 
     activity.sendBroadcast(mediaScanIntent); 
    } 

    private Uri getUri(String currentPhotoPath) { 
     File f = new File(currentPhotoPath); 
     return Uri.fromFile(f); 
    } 
} 
+0

這是因爲你的片段永遠不會在onActivityResult()中得到結果。你必須相應地修改你的活動到這個職位:https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – canihazurcode

回答

1

我嘗試使用意圖和從相同的片段調用onActivetyResult()。但是onActivityResult()永遠不會被調用。

那是因爲你對Activity調用startActivityForResult(),而不是片段。您的onActivityResult()方法會出現在您撥打startActivityForResult()的對象上。所以,如果你想片段有onActivityResult(),請在片段上調用startActivityForResult()。基於這個帖子

+0

我現在調用它的片段和'onActivtityResult() '被召喚。謝謝!但不知何故onActivityResult()中的數據意圖爲空......你知道爲什麼嗎?我把我的數據意圖如下:'takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile)); takePictureIntent.putExtra(Constants.IMAGE_FILE_NAME,currentFileName); fragment.startActivityForResult(takePictureIntent,Constants.TAKE_PHOTO);' –

+0

@HansiHansenbaum:「但不知何故onActivityResult()中的數據意圖爲空......你知道爲什麼嗎? - 它應該是'null'。你知道圖像應該在哪裏('photoFile')。 – CommonsWare

+0

對不起,這些愚蠢的問題。但是,如何從意圖傳遞一個字符串到onActivityResult()? –

2

onActivityResult is not being called in Fragment

嘗試在託管活動

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
} 

這將允許活動結果傳遞未處理你的片段加入。

此外,請確保您撥打fragment.startActivityForResult()而不是activity.startActivityForResult()

相關問題