-1

我在我的应用程序中显示来自 sdcard 的列表视图中的文件。我在 AsyncTask 中这样做。当我单击列表视图中的项目(文件)时,会出现以下错误

11-28 09:39:40.426  26402-26402/app.comser D/StrictMode﹕ StrictMode policy violation; ~duration=3 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
            at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1137)
            at libcore.io.BlockGuardOs.stat(BlockGuardOs.java:292)
            at java.io.File.isDirectory(File.java:524)
            at customize.FilesExplorer$2.onItemClick(FilesExplorer.java:120)
            at android.widget.AdapterView.performItemClick(AdapterView.java:300)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
            at android.widget.AbsListView.onTouchUp(AbsListView.java:3845)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3617)
            at android.view.View.dispatchTouchEvent(View.java:8393)

我添加了异步任务文件代码

 class FilesList_AsyncTask extends AsyncTask<Void,Integer,String>
    {
        File file;
        FilesList_AsyncTask(File file)
        {
            this.file = file;
        }
        @Override
        protected String doInBackground(Void... params)
        {
            System.out.println("-----------" + file);
            File[] filesArray = null;

            filesList=new ArrayList<File>();
            fileNamesList=new ArrayList<String>();

    /*
     * If the selected File is directory and has any files add them to File type of array
    */
            if(file.isDirectory())
            {
                filesArray=file.listFiles();
                selectedFolder=file;
            }

        /*
         * If the File array has any items, add the filenames and files of the items to corresponding arraylist.
         * fileNamesList contains names of files in the folder.
         * filesList contains paths of the files in the folder.
        */
            if(filesArray!=null)
            {
                for (int i = 0; i < filesArray.length; i++)
                {
                    fileNamesList.add(filesArray[i].getName());
                    filesList.add(filesArray[i]);
                    getFileExtension(filesArray[i].getName(),filesArray[i]);
                    // System.out.println("=================="+filesArray[i]);
                }
                Log.d("filenames size...", fileNamesList.size() + "");
                if(fileNamesList.size()>0)
                {
                    filesDialog(fileNamesList, filesList);
                }
                else
                {
                    Toast.makeText(FilesExplorer.this, "No Sub Items Available", Toast.LENGTH_SHORT).show();
                }
            }
            return null;
        }
    }

提前致谢

4

1 回答 1

0

如果 AsyncTask 解决方案不起作用,您可以按如下方式关闭严格模式。

 StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(old)
    .permitDiskWrites()
    .build());
doCorrectStuffThatWritesToDisk();
StrictMode.setThreadPolicy(old);
于 2015-11-28T04:18:09.623 回答