【发布时间】:2019-08-15 12:21:31
【问题描述】:
我正在尝试打开一个已存储在应用数据目录中的 pptx 文件,但打开 ppt 文件时遇到错误。
我已经使用基本方法来获取文件路径,然后是 Uri,从那里将文件 Uri 传递给打开 pptx 文件的意图。
由于我在 ma app-data(电话 - 内部存储)中有文件,我想我不需要使用文件提供程序。但是我仍然使用文件提供程序,但我在打开文件时仍然面临问题
我正在使用具有卡片视图的回收视图,每个视图都有一个打开特定文件的按钮,因此我使用的是适配器,视图持有人......并且在适配器类下可以使用以下 onclick 方法-->onBindViewHolder 函数.
这是用于打开存储的特定文件的onClick函数
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.fOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(holder.mname.getContext().getFilesDir(), "Download");
File pptFile = new File(file,"Introduction.pptx");
Uri uri = FileProvider.getUriForFile(holder.mname.getContext(), "com.example.r_source.fileprovider", pptFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pptx");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
mainActivity.startActivity(intent);
}
});
}
以下是文件提供者的文件路径xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="materials" path="Download/"/>
</paths>
下面是清单文件
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.r_source.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
我得到的错误信息是:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.example.r_source.fileprovider/materials/Introduction.pptx typ=application/pptx flg=0x1 }
【问题讨论】:
标签: java android android-intent android-activity android-file