Question

I've currently got ScrictMode enabled in my app to determine where I can alleviate work on the UI thread and I'm currently getting an onReadFromDisk FileInputStream Violation when I'm creating a new FileInputStream instance and then using it to set the datasource of my MediaPlayer. I created a separate thread to load the file into the FileInputStream instance and set the datasource of my MediaPlayer but ScrictMode is still reporting there is an onReadFromDisk violation when I try create the FileInputStream instance. Code is as follows:

final Handler uiHandler = new Handler(Looper.getMainLooper());

Thread loadInputStream = new Thread(new Runnable() {

                @Override
                public void run() {

                    try {

                        if (m_mediaPlayer == null)
                            m_mediaPlayer = new MediaPlayer();

                        FileInputStream fileInputStream = new FileInputStream(path);
                        m_mediaPlayer.setDataSource(fileInputStream.getFD());

                        fileInputStream.close();

                        m_mediaPlayer.setOnErrorListener(MainActivity.this);
                        m_mediaPlayer.setOnPreparedListener(MainActivity.this);
                        m_mediaPlayer.setOnCompletionListener(MainActivity.this);

                        m_mediaPlayer.prepare();

                    } catch (final Exception e) {

                        uiHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                LogException(e.getMessage() + " (path == " + path + ")", "playVideo()");
                            }                           
                        });
                    }
                }               
            }); 


loadInputStream.run();

I've had a look around SO and found another post which indicated I ought to reset the StrictMode thread policy - which I tried but I'm still getting that violation report.

This is what the LogCat ScrictMode report looks like:

at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1107)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:106)
at libcore.io.IoBridge.open(IoBridge.java:400)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at java.io.FileInputStream.<init>(FileInputStream.java:105)
at com.subdc.subdcmain.MainActivity$6.run(MainActivity.java:440)
at java.lang.Thread.run(Thread.java:856)
at com.subdc.subdcmain.MainActivity.playVideo(MainActivity.java:467)

Any idea as to what I'm going wrong?

Many thanks.

Was it helpful?

Solution 2

You must start() the thread. If you just call run() in main thread, everything will be executed in main thread

OTHER TIPS

To start a thread, call start(), not run(). Right now, you are just running your run() method on the current thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top