android上,在机顶盒插入U盘并读取U盘内容时,当拨出U盘时程序崩溃,如果不读取U盘内容拨出U盘时程序正常,请高人指导!

解决方案 »

  1.   

    你应该重写一个自己的读取流,流的读取本身就是一个装饰者模式。
    重写read方法,每隔几秒就进行检查一下,如果发现找不到u盘就自己throw一个自定义exception,程序来捕获exception,然后在界面上可以弹出error。
      

  2.   

    我是通过广播来检测U盘是是否挂载的,然后再读取U数据加到listview 中:
    Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
    null, null, null, null);拨出U盘时程序崩溃,log:
     W/CursorWrapperInner(26874): Cursor finalized without prior close()
    也没有什么IO异常
      

  3.   

    每次读完以后,调用cursor.close();
    log已经说的很清楚了
      

  4.   

    谢谢各位,当U插入时,系统在扫描U盘(主要是时间太长了,等不下去,就手动button去读取U盘内容),这个过程中如果我拨出U盘,程序就崩溃,下面是logcat:请各位高手,如何解决?10-26 06:15:44.546: I/YuMeAndroidSDK(1567): Notifying Broadcast Event: android.intent.action.MEDIA_BAD_REMOVAL to BSP & Player.
    10-26 06:15:44.546: I/YuMeAndroidPlayer(1567): YuMePlayer_HandleBroadcastEvent(): Invoked.
    10-26 06:15:44.546: I/YuMeAndroidPlayer(1567): YuMePlayer_HandleBroadcastEvent(): Successful.
    10-26 06:15:44.546: I/ScanUSBReceiver(11929): onReceive...
    10-26 06:15:44.546: I/ScanUSBReceiver(11929): android.intent.action.MEDIA_BAD_REMOVAL
    10-26 06:15:44.546: I/ScanUSBReceiver(11929): USB,removed 卸载时执行....
    10-26 06:15:44.546: I/skymp3(1737): upan unmounted , the path is:/mnt/sdb/sdb1
    10-26 06:15:44.566: D/NetlinkEvent(880): NL param 'DEVPATH=/devices/platform/hiusb-ehci.0/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/block/sdb'
    10-26 06:15:44.566: D/NetlinkEvent(880): NL param 'MAJOR=8'
    10-26 06:15:44.566: D/NetlinkEvent(880): NL param 'MINOR=16'
    10-26 06:15:44.566: D/NetlinkEvent(880): NL param 'DEVNAME=sdb'
    10-26 06:15:44.566: D/NetlinkEvent(880): NL param 'DEVTYPE=disk'
    10-26 06:15:44.566: D/NetlinkEvent(880): NL param 'NPARTS=0'
    10-26 06:15:44.566: D/Vold(880): wuxingyu : vm handleBlockEvent
    10-26 06:15:44.566: D/DirVolume(880): wuxingyu : handleBlockEvent begin
    10-26 06:15:44.576: W/InputManagerService(1307): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@418e6908 (uid=10039 pid=11929)
    10-26 06:15:44.726: D/dalvikvm(1307): GC_EXPLICIT freed 474K, 8% free 12879K/13959K, paused 13ms+6ms
    10-26 06:15:44.726: W/MediaScanner(11887): Error opening directory '/mnt/sdb/sdb1/android-sdk-windows/docs/resources/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/logging/', skipping: No such file or directory.
    10-26 06:15:44.726: W/MountService(1307): output =null
    10-26 06:15:44.726: D/MountService(1307): wuxingyu : use the mConnectorSpec[1]
    10-26 06:15:44.726: D/VoldCmdListener(880): volume unmount_bad /mnt/sdb/sdb1 force_and_revert
    10-26 06:15:44.726: D/Vold(880): Volume sdb1 state changing 4 (Mounted) -> 5 (Unmounting)
    10-26 06:15:44.726: W/MountService(1307): output =null
    10-26 06:15:44.766: W/MediaScanner(11887): Error opening directory '/mnt/sdb/sdb1/android-sdk-windows/docs/resources/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/inject/', skipping: No such file or directory.
    10-26 06:15:44.766: W/MediaScanner(11887): Error opening directory '/mnt/sdb/sdb1/android-sdk-windows/docs/resources/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/', skipping: No such file or directory.10-26 06:15:46.506: W/MediaScanner(11887): Error opening directory '/mnt/sdb/sdb1/android-sdk-windows/docs/resources/dashboard/', skipping: No such file or directory.
    10-26 06:15:46.546: D/dalvikvm(1628): GC_FOR_ALLOC freed 649K, 7% free 35352K/37767K, paused 86ms
    10-26 06:15:46.606: E/ProcessKiller(880): Process android.process.media (11887) has open file /mnt/sdb/sdb1
    10-26 06:15:46.606: W/ProcessKiller(880): Sending SIGHUP to process 11887
    10-26 06:15:46.616: D/Zygote(887): Process 11887 terminated by signal (15)
    10-26 06:15:46.616: I/ActivityManager(1307): haveBgApp:true app.setAdj:0
    10-26 06:15:46.616: I/ActivityThread(11929): Removing dead content provider: media
    10-26 06:15:46.616: I/ActivityManager(1307): Process android.process.media (pid 11887) has died.
    10-26 06:15:46.616: W/ActivityManager(1307): Scheduling restart of crashed service com.android.providers.media/.MediaScannerService in 106986ms
    10-26 06:15:46.616: I/ActivityManager(1307): Kill com.video.app (pid 11929): provider com.android.providers.media.MediaProvider in dying process android.process.media
    10-26 06:15:46.636: W/InputDispatcher(1307): channel '41942c70 com.video.app/com.video.app.VideoActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
    10-26 06:15:46.636: E/InputDispatcher(1307): channel '41942c70 com.video.app/com.video.app.VideoActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
    10-26 06:15:46.636: W/InputDispatcher(1307): Attempted to unregister already unregistered input channel '41942c70 com.video.app/com.video.app.VideoActivity (server)'
    10-26 06:15:46.636: I/WindowManager(1307): WINDOW DIED Window{41942c70 com.video.app/com.video.app.VideoActivity paused=false}
    10-26 06:15:46.656: I/ActivityManager(1307): haveBgApp:true app.setAdj:0
    10-26 06:15:46.656: E/JavaBinder(1307): !!! FAILED BINDER TRANSACTION !!!
    10-26 06:15:46.656: I/ActivityManager(1307): Process com.video.app (pid 11929) has died.
    10-26 06:15:46.656: W/ActivityManager(1307): Force removing ActivityRecord{416c0528 com.video.app/.VideoActivity}: app died, no saved state
    10-26 06:15:46.686: E/SurfaceTexture(886): [com.video.app/com.video.app.VideoActivity] dequeueBuffer: SurfaceTexture has been abandoned!
    10-26 06:15:46.806: E/SurfaceTexture(886): [com.nmc.suntv.launcher/com.nmc.suntv.launcher.AppsActivity] dequeueBuffer: can't dequeue multiple buffers without setting the buffer count
    10-26 06:15:46.856: D/dalvikvm(1628): GC_CONCURRENT freed 1338K, 5% free 35950K/37767K, paused 2ms+6ms
    10-26 06:15:47.616: I/Vold(880): /mnt/sdb/sdb1 sucessfully unmounted
    10-26 06:15:47.616: I/Vold(880): /mnt/sdb/sdb1 unmounted sucessfully