列出我的 Android 应用程序打开的所有文件 [英] List all files opened by my Android App

查看:47
本文介绍了列出我的 Android 应用程序打开的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在我的 Xamarin/Android 应用程序中写入文本文件时,我间歇性地收到太多打开的文件异常.我知道这是因为每个 Android 应用程序对它可以打开的文件数量都有限制,显然我的应用程序在某些情况下超过了它.因此,我必须将一些文件打开/没有在某处正确处理它们.我想知道我是否可以以编程方式列出我的应用程序在任何给定时间打开的文件?显然操作系统知道这一点,是否可以在我的应用程序中执行此操作?这将帮助我找到问题.

I am intermittently getting Too many open files exceptions when I try to write to a text file in my Xamarin / Android app. I understand that this is because each Android app has a limit on the number of files it can have open, and obviously my app is exceeding it in some circumstances. Hence I must be leaving some files open / not disposing of them properly somewhere. I would like to know if I can programatically list the files that my app has open at any given time? Obviously the operating system knows this, is it possible to do this from within my app? This will help me find the problem.

感谢 fiddler,这就是我解决它的方法,以防其他人将来需要这样做(在 C#/Xamarin 中):

Thanks to fiddler, this is how I solved it in case anyone else needs to do it in the future (in C# / Xamarin):

private static List<string> m_commandData;

    private static void GetOpenedFiles()
    {
        m_commandData = new List<string>();
        RunCommand("lsof");

        RunCommand("ps | grep TestNoFilesOpen");

    }

    private static void RunCommand(string command)
    {
        string data;
        using (Java.Lang.Process process = Java.Lang.Runtime.GetRuntime().Exec(command))
        {

            Stream stream = process.InputStream;

            StreamReader bodyReader = new StreamReader(stream);

            data = bodyReader.ReadToEnd();
        }
        m_commandData.Add(data);

    }

    private static void WriteCommands()
    {
        for (int i = 0; i < m_commandData.Count; i++)
        {

            using (TextWriter tw = new StreamWriter(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/NoFilesOpen/adb_" + i.ToString() + ".txt"))
            {
                tw.Write(m_commandData[i]);
                tw.Close();
            }
        }

        m_commandData = new List<string>();
    }

推荐答案

您可以使用 Unix ADB shell 中的 ="nofollow">lsof 命令.

You could use the Unix lsof command in the ADB shell.

1) 启动 shell:

1) Start the shell:

$ adb shell

2) 查找您的应用的进程 ID:

2) Find the process ID of your app:

shell@android:/ $ ps | grep <packagename>

3) 列出您的应用打开的文件:

3) List files opened by your app:

shell@android:/ $ lsof <pid>

这篇关于列出我的 Android 应用程序打开的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆