[英]Android intent filer for opening pdf files
I added my pdf viewer app to "Complete action using" list for pdf files. 我将pdf查看器应用程序添加到了pdf文件的“使用完成操作”列表中。 But if I run a file on the list, the app just shut down.
但是,如果我在列表上运行文件,该应用程序将关闭。
I made intent filter in onCreate method and manifest file, but I couldn't figure out how to fix this error. 我在onCreate方法和清单文件中创建了意图过滤器,但我不知道如何解决此错误。
PdfActivity.java PdfActivity.java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf); //xml file for pdf view page
Intent intent = getIntent();
if(intent != null) {
if(Intent.ACTION_VIEW.equals(intent.getAction())) {
startActivity(intent);
}
}
init();
}
AndroidManifest.xml AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PdfActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content" />
<data android:scheme="file" />
<data android:mimeType="application/pdf" />
</intent-filter>
</activity>
</application>
Please help me in this problem. 请帮我解决这个问题。
Try this 尝试这个
//Method to generate a MIME Type
private static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
//Method to generate File URI
private static Uri getFileUri(String filePath) {
Uri fileUri = null;
File file = new File(filePath);
try {
if (Build.VERSION.SDK_INT >= 24) {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}
fileUri = Uri.fromFile(file);
} catch (Exception e) {
e.printStackTrace();
}
return fileUri;
}
//Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(getFileUri(file_path), getMimeType(file_path));
startActivity(intent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.