启动另一个活动以查看文件时活动被破坏

时间:2014-02-20 07:07:18

标签: 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();
    }
}

这样工作正常,因为我得到app文件类型弹出窗口的文件类型有多个与之相关的应用程序(例如,kingsoft办公室和极地办公室的word文件)或直接文件打开文件类型只有一个与之相关的应用程序(HTMLViewer for html file)。

我不明白的是,我的主要活动是在调用文件查看器活动后的几分钟内被破坏。我已通过将日志放入onPause()onStop()onDestroy()来检查此问题。因此,在完成文件查看后,当我按下后退键,而不是回到应用程序时,它会导航到主菜单。

我尝试使用startActivityForResult(intent, 1);代替startActivity(intent);,希望它能保留我的主要活动,因为它会等待结果,但无济于事。

请注意,我已在清单文件和

中添加了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)

2 个答案:

答案 0 :(得分:1)

将这些行添加到Intent并尝试启动活动

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链接

希望它会对你有所帮助。

答案 1 :(得分:0)

活动可以随时被破坏。如果某人有一些数据要保留,可以按如下方式执行:

class MyActivity {

private String mMyString;

public void onSaveInstanceState(Bundle b) {

b.putString("MYDATA", mMyString);
super.onSaveInstanceState(b);
}

public void onCreate(Bundle savedState) {
super.onCreate(savedState)
if(savedState != null) {
 mMyString = savedState.getString("MYDATA");
}

}

您可以通过这种方式创建自己的子类private static class State implements Serializable并将其保存在onSaveInstanceState()