gpt4 book ai didi

java - Android:从 Fragment 到 Activity 的 Intent 不起作用

转载 作者:行者123 更新时间:2023-11-30 11:10:37 25 4
gpt4 key购买 nike

我有一个调用自定义拍照 Activity 的 fragment 。

传递给 Activity 调用的 Intent 额外不起作用。如果我打印 logcat,我可以在 fragment 中看到 Intent 。但是 Activity 没有收到它。

fragment :

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

Activity :

@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);

日志:

来自 fragment :

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

来自 Activity 日志:

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

最佳答案

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

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

关于java - Android:从 Fragment 到 Activity 的 Intent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com