首先,我是Android的编程新手。 我想从图库中选择一个图像并将其上传。 所有这一切都来自片段。 问题是,当我从片段中选择图像时,返回到First Fragment,而不是调用它的片段。 我搜索了这个,但选择图片后没有返回 onActivityResult 这https://inthecheesefactory.com/blog/how-to-fix-nested-fragment-onactivityresult-issue/en对我不起作用 感谢
这是我的代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("fragment","fragment");
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
// Get the url from data
selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
//Log.i(TAG, "Image Path : " + path);
// Set the image in ImageView
}
}
}
}
void openImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
更新:已解决
除了调用startActivityForResult
之外,我都说过帖子 void openImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 122) {
// Do your job
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
// Get the url from data
selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
//Log.i(TAG, "Image Path : " + path);
// Set the image in ImageView
guardar();
}
}
}
}
}
答案 0 :(得分:0)
阅读该帖子......
收到的所有结果都将在一个地方处理:片段放置的活动的onActivityResult。
因此,将onActivityResult移动到Activity。
有道理,因为Activity启动了intent,而不是Fragment本身。
getActivity().startActivityForResult