如何以编程方式打开 android 中的隐藏文件?

how to open a hidden file in android programmatically?

在我的应用程序中,我使用以下代码创建了一个隐藏的文本文件:

logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt");

if(!logfile.exists()){
    try {
        logfile.createNewFile();

        //Toast.makeText(SimpleIME.this,"File created...",Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(SimpleIME.this,"IOException : "+e.getMessage(),Toast.LENGTH_SHORT).show();
    }
}

这很好用。它创建一个隐藏文件。然后我想在按下名为 viewlog.

的按钮时再次打开该文本文件

viewlog 的代码如下。

viewlog.setOnClickListener(new OnClickListener() {

@Override
    public void onClick(View arg0) {

        logfile =    new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt");
         Uri uri = Uri.parse("file://" + logfile.getAbsolutePath());
         Intent intent = new Intent(Intent.ACTION_VIEW);
         intent.setData(uri);
         startActivity(intent);

    }

});

因此,当我 运行 这个应用程序并单击此 viewlog 按钮时,它会强制关闭应用程序。

那么如何解决这个问题?

原因:android.content.ActivityNotFoundException:未找到 Activity 处理 Intent

    logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt");
    Uri uri = Uri.parse("file://" + logfile.getAbsolutePath());
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "text/plain");
    startActivity(intent);