Android:从Fragment到Activity的意图不起作用

时间:2014-12-26 03:15:27

标签: java android android-intent android-activity android-fragments

我有一个调用自定义拍照活动的片段。

额外传递给活动调用的意图不起作用。如果我打印logcat,我可以看到片段中的意图。但活动没有收到它。

片段:

Intent intent = new Intent();           
intent.setClass(getActivity(), CameraActivity.class);   


Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra("outFile", fileUri);                    

    Log.e(TAG, "" +
            "------------------------------------------------------ " +
            "fileUri intent => " + fileUri);

startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

Intent intent = getIntent();
outFile = intent.getStringExtra("outFile");

String test = getIntent().getExtras().getString("outFile");

Log.i("CameraActivity", "" +
        "- " +
        "test intent => " + test);


Log.e("CameraActivity", "" +
        "- " +
        "intent.getStringExtra(outFile) => " + intent.getStringExtra("outFile"));  

Log.e("CameraActivity", "" +
        "- " +
        "outFile intent => " + outFile);

logcat的:

来自fragemnt:

12-26 11:12:21.399: E/PostProductFragment(3563): - fileUri intent => file:///mnt/sdcard/Pictures/MyCameraApp/IMG_20141226_111221.jpg

来自活动日志:

12-26 11:12:21.449: I/CameraActivity(3563): - test intent => null
12-26 11:12:21.449: E/CameraActivity(3563): - intent.getStringExtra(outFile) => null
12-26 11:12:21.449: E/CameraActivity(3563): - outFile intent => null

1 个答案:

答案 0 :(得分:1)

我没有正确创建文件路径。

private File createFolders() {

    File baseDir;

    if ( android.os.Build.VERSION.SDK_INT < 8 ) {
        baseDir = Environment.getExternalStorageDirectory();
    } else {
        baseDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES );
    }

    if ( baseDir == null ) return Environment.getExternalStorageDirectory();

    Log.d( LOG_TAG, "Pictures folder: " + baseDir.getAbsolutePath() );
    File aviaryFolder = new File( baseDir, FOLDER_NAME );

    if ( aviaryFolder.exists() ) return aviaryFolder;
    if ( aviaryFolder.mkdirs() ) return aviaryFolder;

    return Environment.getExternalStorageDirectory();
}