[英]Action view intent cannot open image in files dir with fileProvider
它是什么,我的應用程序的文件目錄中有一個圖像。 然后我想用意圖、動作視圖打開它。
我似乎已經做了很多年了,它只是不想為我工作。
我有以下代碼:
private void changeInputFileViewer(File file){
boolean noApp = false;
file = new File(getFilesDir()+"/test.png");
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(FileProvider.getUriForFile(getApplicationContext(), "com.example.mayzom", file), "image/png");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
} catch ( ActivityNotFoundException e) {
e.printStackTrace();
noApp = true;
}
if (noApp){
Toast.makeText(this, getString(R.string.noApp), Toast.LENGTH_SHORT).show();
}
}
顯現:
<provider
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:authorities="com.example.mayzom"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
提供程序路徑: <?xml version="1.0" encoding="utf-8"?> <paths> <files-path name="files" path="."/> </paths>
該圖像存在於設備文件資源管理器中,路徑如下: /data/data/com.example.mayzom/files/test.png 。
我在網上查看了以前錯誤的修復程序,因此,我的 onCreate 中有以下代碼: StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build());
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build());
也許要添加比評論中更令人滿意的答案。
您需要創建一個文件提供者。 您可以在此處找到指南,之后您需要以下代碼來滿足您的意圖
boolean noApp = false;
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(FileProvider.getUriForFile(getApplicationContext(), getPackageName()+".provider", file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} catch ( ActivityNotFoundException e) {
e.printStackTrace();
noApp = true;
}
if (noApp){
Toast.makeText(this, getString(R.string.noApp), Toast.LENGTH_SHORT).show();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.