java - 启动另一个 Activity 以查看文件时 Activity 被破坏

标签 java android android-activity

我正在执行如下文件查看操作:

private void openFile(File file, String format) {
    try {
        if (file.exists()) {
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            String mimeType = "*/*";
            if (format.length() > 0) {
                mimeType = MimeTypeMap.getSingleton()
                        .getMimeTypeFromExtension(format);
                if (mimeType == null)
                    mimeType = "*/*";
            } else
                mimeType = "*/*";
            intent.setDataAndType(path, mimeType);

            try {
                startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(LoginSuccessActivity.this,
                        constants.Error_NoCompatibleApp, Toast.LENGTH_SHORT)
                        .show();
                e.printStackTrace();
            }
        } else {
            Toast.makeText(LoginSuccessActivity.this,
                    constants.Error_DocNotFound, Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这工作正常,因为我为具有多个相关应用程序的文件类型弹出了应用程序选择器弹出窗口(例如,kingsoft office 和 polaris office 用于 word 文件)或者直接为仅具有文件类型的文件打开文件一个与之相关的应用程序(HTML 文件的 HTMLViewer)。

我不明白的是我的主要 Activity 在调用文件查看器 Activity 后不久就被破坏了。我通过在 onPause()onStop()onDestroy() 中放置日志来检查这一点。因此,在完成文件查看后,当我按下返回键时,它并没有返回到应用程序,而是导航到了主菜单。

我尝试使用 startActivityForResult(intent, 1); 而不是 startActivity(intent); 希望它会保留我的主要 Activity ,因为它将等待结果,但无济于事。

请注意,我在 list 文件中添加了 android:configChanges="orientation|screenSize"

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

在java文件中。

我做错了什么吗?

感谢任何帮助。

有时,请注意,“有时”,我会在日志中收到以下消息:

 file:// Uri exposed through Intent.getData()
 java.lang.Throwable: file:// Uri exposed through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1597)
    at android.net.Uri.checkFileUriExposed(Uri.java:2338)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:7194)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1418)
    at android.app.Activity.startActivityForResult(Activity.java:3423)
    at android.app.Activity.startActivityForResult(Activity.java:3384)
    at android.app.Activity.startActivity(Activity.java:3626)
    at android.app.Activity.startActivity(Activity.java:3594)
    at com.android.internal.app.ResolverActivity.onIntentSelected(ResolverActivity.java:407)
    at com.android.internal.app.ResolverActivity.startSelected(ResolverActivity.java:299)
    at com.android.internal.app.ResolverActivity.onButtonClick(ResolverActivity.java:289)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at android.view.View$1.onClick(View.java:3809)
    at android.view.View.performClick(View.java:4424)
    at android.view.View$PerformClick.run(View.java:18383)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4998)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    at dalvik.system.NativeStart.main(Native Method)

最佳答案

将这些行添加到您的 Intent 并尝试启动 Activity,

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType(Your_MimeType);

我在我的应用程序中使用这段代码

String MimeType = URLConnection.guessContentTypeFromName(myFile.getAbsolutePath());

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType(MimeType);
Uri uri_path = Uri.fromFile(myFile);
intent.setDataAndType(uri_path, MimeType);
try
{
    _context.startActivity(intent);
}
catch(ActivityNotFoundException e)
{
    Toast.makeText(_context,"Activity Not Found..", Toast.LENGTH_SHORT).show();
}

引用this链接也可以更好地理解

希望对你有所帮助。

关于java - 启动另一个 Activity 以查看文件时 Activity 被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21900643/

相关文章:

java - 使用函数调用 Java SOURCE

java - ASCII-错误消息,找不到符号

java - 如何在各个eclipse插件之间共享依赖JAR?

android - 如何检测 Android 中的用户交互

Android - 视频重新启动或恢复

java - 等到elementToBeClickable不是其他元素会收到点击异常的解决方案

android - 如何获取 dataclient.getDataItem(Uri) 自己的 node_id?

android - AlertDialog 中的自定义 Listview 隐藏对话框的消息?

Activity 保存的 android:support:fragments 越来越大并导致 TransactionTooLargeException

java - Android 超时监听器