提问人:Guillaume Pailloux 提问时间:11/14/2023 更新时间:11/14/2023 访问量:18
在另一个片段中打开 pdf
Open a pdf in another fragment
问:
您好,我有一个android(java)应用程序项目,我的目标是使用API大声朗读pdf文件。但是,我在一开始显示此pdf时遇到了问题,当我在手机文件中选择一个按钮并按下按钮以在另一个片段中打开它时,pdf可以很好地打开。
pdf打开得很好,但我无法返回带有导航栏的选择片段。但是,我可以进入设置片段。你知道问题可能来自哪里吗,如果你有修复程序,我很想听听,因为我找不到解决方案。:/
public class LibraryFragment extends Fragment {
private FragmentLibraryBinding binding;
private ActivityResultLauncher<Intent> filePickerLauncher;
private Uri selectedFileUri;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
LibraryViewModel dashboardViewModel =
new ViewModelProvider(this).get(LibraryViewModel.class);
binding = FragmentLibraryBinding.inflate(inflater, container, false);
View root = binding.getRoot();
final TextView textView = binding.textLibrary;
dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
return root;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Button button = getView().findViewById(R.id.button);
Button button2 = getView().findViewById(R.id.button2);
//Button button3 = getView().findViewById(R.id.button3);
filePickerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
if (data != null) {
selectedFileUri = data.getData();
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
filePickerLauncher.launch(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedFileUri != null) {
Bundle bundle = new Bundle();
bundle.putString("pdfUri", selectedFileUri.toString());
Navigation.findNavController(v).navigate(R.id.action_navigation_library_to_navigation_play, bundle);
} else {
Toast.makeText(getActivity(), "No file selected.", Toast.LENGTH_SHORT).show();
}
}
});
public class PlayFragment extends Fragment {
private FragmentPlayBinding binding;
private PDFView pdfView;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
PlayViewModel playViewModel =
new ViewModelProvider(this).get(PlayViewModel.class);
binding = FragmentPlayBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
pdfView = view.findViewById(R.id.PDFView);
if (getArguments() != null) {
String pdfUri = getArguments().getString("pdfUri");
if (pdfUri != null) {
pdfView.fromUri(Uri.parse(pdfUri)).load();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/navigation_library">
<fragment
android:id="@+id/navigation_settings"
android:name="com.example.storeteller.ui.settings.SettingsFragment"
android:label="@string/title_settings"
tools:layout="@layout/fragment_settings" />
<fragment
android:id="@+id/navigation_library"
android:name="com.example.storeteller.ui.library.LibraryFragment"
android:label="@string/title_library"
tools:layout="@layout/fragment_library" >
<action
android:id="@+id/action_navigation_library_to_navigation_play"
app:destination="@id/navigation_play" />
</fragment>
<fragment
android:id="@+id/navigation_play"
android:name="com.example.storeteller.ui.play.PlayFragment"
android:label="@string/title_play"
tools:layout="@layout/fragment_play" >
<action
android:id="@+id/action_navigation_play_to_navigation_library"
app:destination="@id/navigation_library"
app:popUpTo="@id/navigation_library"
app:popUpToInclusive="true" />
</fragment>
</navigation>
答: 暂无答案
评论