Android URI错误 java.io.FileNotFoundException: 未找到内容提供程序: http://

huangapple 未分类评论47阅读模式
英文:

Android URI error java.io.FileNotFoundException: No content provider: http://

问题

  1. Bundle fileParams = getIntent().getExtras();
  2. filePosition = fileParams.getInt("index");
  3. allFiles = fileParams.getStringArrayList("arrayList");
  4. photoOpenerAdapter = new PhotoOpenerAdapter(allFiles, cntxt);
  5. viewPager = (ViewPager) findViewById(R.id.viewPager);
  6. viewPager.setAdapter(photoOpenerAdapter);
  7. viewPager.setCurrentItem(filePosition);
  8. viewPager.setOffscreenPageLimit(allFiles.size());
  9. viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  10. @Override
  11. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  12. viewPager.post(new Runnable() {
  13. @Override
  14. public void run() {
  15. if (!loadedInitial) {
  16. loadedInitial = true;
  17. String filePath = allFiles.get(position);
  18. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  19. if (fileType.equals("video"))
  20. startVideoPlayer(position);
  21. }
  22. }
  23. });
  24. }
  25. @Override
  26. public void onPageSelected(int position) {
  27. pauseVideoPlayer();
  28. String filePath = allFiles.get(position);
  29. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  30. if (fileType.equals("video")) {
  31. startVideoPlayer(position);
  32. }
  33. }
  34. @Override
  35. public void onPageScrollStateChanged(int state) {
  36. }
  37. });
  38. private void pauseVideoPlayer() {
  39. if (!(videoView == null)) {
  40. videoView.pause();
  41. }
  42. }
  43. public static void startVideoPlayer(int position) {
  44. View videoLayout = viewPager.getChildAt(position);
  45. videoView = (VideoView) videoLayout.findViewById(R.id.videoPlayer);
  46. if (!(videoView == null)) {
  47. videoView.requestFocus();
  48. videoView.start();
  49. }
  50. }
  1. @NonNull
  2. @Override
  3. public Object instantiateItem(@NonNull ViewGroup container, final int position) {
  4. layoutInflater = LayoutInflater.from(context);
  5. String imageUrl = postFilesLists.get(position);
  6. String fileType = functions.getmInstance(context).checkFileType(imageUrl.toLowerCase());
  7. if (fileType.equals("video")) {
  8. view = layoutInflater.inflate(R.layout.video_player, container, false);
  9. VideoView videoView = (VideoView) view.findViewById(R.id.videoPlayer);
  10. Uri uri = Uri.parse(imageUrl);
  11. videoView.setVideoURI(uri);
  12. } else {
  13. view = layoutInflater.inflate(R.layout.photo_opener, container, false);
  14. ImageView imageView = (ImageView) view.findViewById(R.id.image);
  15. imageLoader.displayImage(imageUrl, imageView);
  16. }
  17. container.addView(view);
  18. return view;
  19. }

This code handles the creation and display of images and videos using a ViewPager. It defines methods to pause and play video playback and utilizes an adapter to create the view items. The issue you're facing with the "java.io.FileNotFoundException: No content provider" error might be due to incorrect handling of video URLs. Please make sure the video URLs are properly formatted and accessible.

英文:

I am creating something like a facebook post. Where a user can upload images and videos, and also view them. So I create an arrayList of the files and if the user click any image or video, it take them to a new activity to display all files in a viewpager, passing the arrayList and the index of the clicked file as parameters, so I can use the arrayList to set my adapter and the position of my viewpager to the index of the clicked file.

So here is my activity which displays the files

  1. Bundle fileParams = getIntent().getExtras();
  2. filePosition = fileParams.getInt("index");
  3. allFiles = fileParams.getStringArrayList("arrayList");
  4. photoOpenerAdapter = new PhotoOpenerAdapter(allFiles, cntxt);
  5. viewPager = (ViewPager) findViewById(R.id.viewPager);
  6. viewPager.setAdapter(photoOpenerAdapter);
  7. viewPager.setCurrentItem(filePosition);
  8. viewPager.setOffscreenPageLimit(allFiles.size());
  9. viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  10. @Override
  11. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  12. viewPager.post(new Runnable() {
  13. @Override
  14. public void run() {
  15. //loadedInitial is a boolean value to make this thread run only once
  16. if(!loadedInitial) {
  17. loadedInitial = true;
  18. String filePath = allFiles.get(position);
  19. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  20. if(fileType == "video")
  21. startVideoPlayer(position);
  22. }
  23. }
  24. });
  25. }
  26. @Override
  27. public void onPageSelected(int position) {
  28. pauseVideoPlayer();
  29. String filePath = allFiles.get(position);
  30. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  31. if(fileType == "video") {
  32. startVideoPlayer(position);
  33. }
  34. }
  35. @Override
  36. public void onPageScrollStateChanged(int state) {
  37. }
  38. });
  39. private void pauseVideoPlayer() {
  40. if(!(videoView == null)){
  41. videoView.pause();
  42. }
  43. }
  44. public static void startVideoPlayer(int position) {
  45. View videoLayout = viewPager.getChildAt(position);
  46. videoView = (VideoView) videoLayout.findViewById(R.id.videoPlayer);
  47. if(!(videoView == null)) {
  48. videoView.requestFocus();
  49. videoView.start();
  50. }
  51. }
  52. }

And here is the instantiateItem thread of my PhotoOpenerAdapter.java class

  1. @NonNull
  2. @Override
  3. public Object instantiateItem(@NonNull ViewGroup container, final int position) {
  4. layoutInflater = LayoutInflater.from(context);
  5. String imageUrl = postFilesLists.get(position);
  6. String fileType = functions.getmInstance(context).checkFileType(imageUrl.toLowerCase());
  7. if(fileType == "video") {
  8. view = layoutInflater.inflate(R.layout.video_player, container, false);
  9. VideoView videoView = (VideoView) view.findViewById(R.id.videoPlayer);
  10. Uri uri = Uri.parse(imageUrl);
  11. videoView.setVideoURI(uri);
  12. } else{
  13. view = layoutInflater.inflate(R.layout.photo_opener, container, false);
  14. ImageView imageView = (ImageView) view.findViewById(R.id.image);
  15. imageLoader.displayImage(imageUrl, imageView);
  16. }
  17. container.addView(view);
  18. return view;
  19. }

This code works fine but when the filePosition is a video file, and I try to use the onPageScrolled thread to load the video Url, I get the error java.io.FileNotFoundException: No content provider: http:// which is coming from the line Uri uri = Uri.parse(imageUrl);, but if the filePosition is an image file, and I scroll to a video file, onPageSelected plays the video successfully.

Please I don't what am doing wrong

Here is the complete Stack Trace

  1. 04/07 11:30:40: Launching 'app' on Pixel 2 API 29.
  2. $ adb shell am start -n "com.pixtanta.app/com.pixtanta.app.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
  3. Waiting for process to come online...
  4. Connected to process 2259 on device 'Pixel_2_API_29 [emulator-5554]'.
  5. Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
  6. I/om.pixtanta.ap: Not late-enabling -Xcheck:jni (already on)
  7. W/om.pixtanta.ap: Suspending all threads took: 11.490ms
  8. E/om.pixtanta.ap: Unknown bits set in runtime_flags: 0x8000
  9. W/om.pixtanta.ap: Unexpected CPU variant for X86 using defaults: x86
  10. I/MultiDex: VM with version 2.1.0 has multidex support
  11. Installing application
  12. VM has multidex support, MultiDex support library is disabled.
  13. W/RenderThread: type=1400 audit(0.0:270): avc: denied { write } for name="property_service" dev="tmpfs" ino=6344 scontext=u:r:untrusted_app:s0:c139,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.pixtanta.app
  14. D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
  15. W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
  16. D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
  17. D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
  18. D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
  19. W/om.pixtanta.ap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
  20. W/om.pixtanta.ap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
  21. W/om.pixtanta.ap: Accessing hidden method Landroid/graphics/FontFamily;-><init>()V (greylist, reflection, allowed)
  22. Accessing hidden method Landroid/graphics/FontFamily;->addFontFromAssetManager(Landroid/content/res/AssetManager;Ljava/lang/String;IZIII[Landroid/graphics/fonts/FontVariationAxis;)Z (greylist, reflection, allowed)
  23. Accessing hidden method Landroid/graphics/FontFamily;->addFontFromBuffer(Ljava/nio/ByteBuffer;I[Landroid/graphics/fonts/FontVariationAxis;II)Z (greylist, reflection, allowed)
  24. Accessing hidden method Landroid/graphics/FontFamily;->freeze()Z (greylist, reflection, allowed)
  25. Accessing hidden method Landroid/graphics/FontFamily;->abortCreation()V (greylist, reflection, allowed)
  26. Accessing hidden method Landroid/graphics/Typeface;->createFromFamiliesWithDefault([Landroid/graphics/FontFamily;Ljava/lang/String;II)Landroid/graphics/Typeface; (greylist, reflection, allowed)
  27. D/HostConnection: HostConnection::get() New Host Connection established 0xdf3462d0, tid 2311
  28. HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2
  29. W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
  30. D/EGL_emulation: eglCreateContext: 0xdf31a240: maj 2 min 0 rcv 2
  31. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  32. W/Gralloc3: mapper 3.x is not supported
  33. D/HostConnection: createUnique: call
  34. D/HostConnection: HostConnection::get() New Host Connection established 0xdf346410, tid 2311
  35. D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2
  36. D/eglCodecCommon: allocate: Ask for block of size 0x1000
  37. allocate: ioctl allocate returned offset 0x3ffff6000 size 0x2000
  38. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  39. I/om.pixtanta.ap: Background concurrent copying GC freed 4031(1010KB) AllocSpace objects, 2(40KB) LOS objects, 52% free, 1374KB/2910KB, paused 470us total 316.229ms
  40. W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@5f8eb60
  41. I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.
  42. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  43. W/om.pixtanta.ap: Verification of java.lang.String okio.internal.ByteStringKt.commonHex(okio.ByteString) took 232.844ms
  44. I/OpenGLRenderer: Davey! duration=1006ms; Flags=1, IntendedVsync=10885691977642, Vsync=10886258644286, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=10886267267970, AnimationStart=10886267414940, PerformTraversalsStart=10886268302090, DrawStart=10886442318860, SyncQueued=10886448317080, SyncStart=10886459965410, IssueDrawCommandsStart=10886460230610, SwapBuffers=10886678824180, FrameCompleted=10886710235330, DequeueBufferDuration=75000, QueueBufferDuration=573000,
  45. W/om.pixtanta.ap: Verification of java.lang.String okio.internal.ByteStringKt.commonToString(okio.ByteString) took 109.047ms
  46. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  47. I/chatty: uid=10139(com.pixtanta.app) RenderThread identical 2 lines
  48. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  49. W/om.pixtanta.ap: Verification of okio.Buffer okio.Buffer.writeUtf8(java.lang.String, int, int) took 103.361ms
  50. W/om.pixtanta.ap: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setUseSessionTickets(Z)V (greylist,core-platform-api, reflection, allowed)
  51. Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setHostname(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
  52. Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (greylist,core-platform-api, reflection, allowed)
  53. Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([B)V (greylist,core-platform-api, reflection, allowed)
  54. W/om.pixtanta.ap: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, reflection, allowed)
  55. Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
  56. Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, reflection, allowed)
  57. D/NetworkSecurityConfig: No Network Security Config specified, using platform default
  58. W/om.pixtanta.ap: Verification of okio.Source okhttp3.internal.http1.Http1ExchangeCodec.newUnknownLengthSource() took 106.135ms
  59. I/om.pixtanta.ap: NativeAlloc concurrent copying GC freed 8287(782KB) AllocSpace objects, 7(140KB) LOS objects, 49% free, 2343KB/4686KB, paused 1.498ms total 225.137ms
  60. W/System: A resource failed to call close.
  61. I/chatty: uid=10139(com.pixtanta.app) FinalizerDaemon identical 2 lines
  62. W/System: A resource failed to call close.
  63. V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@5dd4fad): Cookies: null
  64. makeHTTPConnection: CookieManager created: java.net.CookieManager@a4fd2e2
  65. makeHTTPConnection(android.media.MediaHTTPService@5dd4fad): cookieHandler: java.net.CookieManager@a4fd2e2 Cookies: null
  66. I/om.pixtanta.ap: NativeAlloc concurrent copying GC freed 4930(489KB) AllocSpace objects, 23(460KB) LOS objects, 49% free, 2754KB/5508KB, paused 3.218ms total 327.970ms
  67. W/System: A resource failed to call close.
  68. I/chatty: uid=10139(com.pixtanta.app) FinalizerDaemon identical 2 lines
  69. W/System: A resource failed to call close.
  70. I/om.pixtanta.ap: JIT allocated 70KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
  71. W/System: A resource failed to call close.
  72. I/chatty: uid=10139(com.pixtanta.app) FinalizerDaemon identical 22 lines
  73. W/System: A resource failed to call close.
  74. I/OpenGLRenderer: Davey! duration=1410ms; Flags=0, IntendedVsync=10888523779635, Vsync=10888923779619, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=10888932055650, AnimationStart=10888932332760, PerformTraversalsStart=10888932378740, DrawStart=10889814355560, SyncQueued=10889856339460, SyncStart=10889856637920, IssueDrawCommandsStart=10889859104780, SwapBuffers=10889924611950, FrameCompleted=10889935046350, DequeueBufferDuration=92000, QueueBufferDuration=1623000,
  75. I/om.pixtanta.ap: NativeAlloc concurrent copying GC freed 9429(653KB) AllocSpace objects, 15(300KB) LOS objects, 49% free, 3684KB/7369KB, paused 3.468ms total 426.579ms
  76. W/System: A resource failed to call close.
  77. I/chatty: uid=10139(com.pixtanta.app) FinalizerDaemon identical 7 lines
  78. W/System: A resource failed to call close.
  79. W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@49b7a99
  80. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  81. W/MediaPlayer: Couldn't open http://192.168.58.1/...
  82. java.io.FileNotFoundException: No content provider: http://192.168.58.1/uploads/video/PXT-IMG-1586206898-82ecc8ad45.mp4
  83. at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1673)
  84. at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1503)
  85. at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1420)
  86. at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1101)
  87. at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1073)
  88. at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1097)
  89. at android.widget.VideoView.openVideo(VideoView.java:412)
  90. at android.widget.VideoView.setVideoURI(VideoView.java:287)
  91. at android.widget.VideoView.setVideoURI(VideoView.java:270)
  92. at com.pixtanta.app.PhotoOpenerAct.startVideoPlayer(PhotoOpenerAct.java:102)
  93. at com.pixtanta.app.PhotoOpenerAct$1.onPageSelected(PhotoOpenerAct.java:76)
  94. at androidx.viewpager.widget.ViewPager.dispatchOnPageSelected(ViewPager.java:1947)
  95. at androidx.viewpager.widget.ViewPager.scrollToItem(ViewPager.java:686)
  96. at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:670)
  97. at androidx.viewpager.widget.ViewPager.onTouchEvent(ViewPager.java:2263)
  98. at android.view.View.dispatchTouchEvent(View.java:13415)
  99. at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3054)
  100. at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2741)
  101. at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
  102. at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
  103. at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
  104. at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
  105. at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
  106. at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
  107. at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
  108. at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
  109. at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
  110. at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
  111. at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
  112. at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
  113. at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:465)
  114. at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1849)
  115. at android.app.Activity.dispatchTouchEvent(Activity.java:3993)
  116. at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
  117. at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:423)
  118. at android.view.View.dispatchPointerEvent(View.java:13674)
  119. at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5482)
  120. at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5285)
  121. at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
  122. at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
  123. at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
  124. at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4947)
  125. at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
  126. at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5004)
  127. at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
  128. at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
  129. at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
  130. at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
  131. at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
  132. at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7505)
  133. at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7474)
  134. W/MediaPlayer: at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7435)
  135. at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7630)
  136. at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
  137. at android.os.MessageQueue.nativePollOnce(Native Method)
  138. at android.os.MessageQueue.next(MessageQueue.java:336)
  139. at android.os.Looper.loop(Looper.java:174)
  140. at android.app.ActivityThread.main(ActivityThread.java:7356)
  141. at java.lang.reflect.Method.invoke(Native Method)
  142. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
  143. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
  144. V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@983af96): Cookies: null
  145. V/MediaHTTPService: makeHTTPConnection: CookieHandler (java.net.CookieManager@a4fd2e2) exists.
  146. makeHTTPConnection(android.media.MediaHTTPService@983af96): cookieHandler: java.net.CookieManager@a4fd2e2 Cookies: null
  147. D/MediaPlayerNative: getMetadata
  148. W/MediaPlayerNative: info/warning (3, 0)
  149. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  150. V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
  151. cleanDrmObj: mDrmObj=null mDrmSessionId=null
  152. resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
  153. cleanDrmObj: mDrmObj=null mDrmSessionId=null
  154. W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@24b04a0
  155. D/EGL_emulation: eglMakeCurrent: 0xdf31a240: ver 2 0 (tinfo 0xdf30f110)
  156. Process 2259 terminated.

答案1

得分: 0

最终,我搞定了...问题是在ViewPager加载适配器之前设置了ViewPager的当前项。所以我必须在ViewPager完成加载后设置它。

  1. Bundle fileParams = getIntent().getExtras();
  2. filePosition = fileParams.getInt("index");
  3. allFiles = fileParams.getStringArrayList("arrayList");
  4. photoOpenerAdapter = new PhotoOpenerAdapter(allFiles, cntxt);
  5. viewPager = (ViewPager) findViewById(R.id.viewPager);
  6. viewPager.setAdapter(photoOpenerAdapter);
  7. // 这里我移除了viewPager.setCurrentItem(filePosition),以便在ViewPager完成加载后设置它
  8. viewPager.setOffscreenPageLimit(allFiles.size());
  9. viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  10. @Override
  11. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  12. viewPager.post(new Runnable() {
  13. @Override
  14. public void run() {
  15. if (!loadedInitial) {
  16. loadedInitial = true;
  17. String filePath = allFiles.get(position);
  18. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  19. if (fileType.equals("video"))
  20. startVideoPlayer(position);
  21. // 这里ViewPager已经加载完成,所以我调用方法来实现viewPager.setCurrentItem(filePosition)
  22. setViewPagerCurrentItem();
  23. }
  24. }
  25. });
  26. }
  27. @Override
  28. public void onPageSelected(int position) {
  29. pauseVideoPlayer();
  30. String filePath = allFiles.get(position);
  31. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  32. if (fileType.equals("video")) {
  33. videoView = null;
  34. startVideoPlayer(position);
  35. }
  36. }
  37. @Override
  38. public void onPageScrollStateChanged(int state) {
  39. }
  40. });
  41. // 实现viewPager.setCurrentItem(filePosition)的方法
  42. private void setViewPagerCurrentItem() {
  43. viewPager.setCurrentItem(filePosition);
  44. }
  45. private void pauseVideoPlayer() {
  46. if (!(videoView == null)) {
  47. videoView.pause();
  48. }
  49. }
  50. public static void startVideoPlayer(int position) {
  51. View videoLayout = viewPager.getChildAt(position);
  52. videoView = (VideoView) videoLayout.findViewById(R.id.videoPlayer);
  53. if (!(videoView == null)) {
  54. videoView.requestFocus();
  55. videoView.start();
  56. }
  57. }

希望这对你有所帮助。

英文:

Finnaly I got it working... the problem was setting the ViewPager's current item before the ViewPager loaded the adapter. So I had to set that after the ViewPager has finished loading

  1. Bundle fileParams = getIntent().getExtras();
  2. filePosition = fileParams.getInt("index");
  3. allFiles = fileParams.getStringArrayList("arrayList");
  4. photoOpenerAdapter = new PhotoOpenerAdapter(allFiles, cntxt);
  5. viewPager = (ViewPager) findViewById(R.id.viewPager);
  6. viewPager.setAdapter(photoOpenerAdapter);
  7. //here I removed the viewPager.setCurrentItem(filePosition) to set it after the ViewPager has finished loading
  8. viewPager.setOffscreenPageLimit(allFiles.size());
  9. viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  10. @Override
  11. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  12. viewPager.post(new Runnable() {
  13. @Override
  14. public void run() {
  15. if(!loadedInitial) {
  16. loadedInitial = true;
  17. String filePath = allFiles.get(position);
  18. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  19. if(fileType == "video")
  20. startVideoPlayer(position);
  21. // here the ViewPager has finished loading so I call the method to implement the viewPager.setCurrentItem(filePosition)
  22. setViewPagerCurrentItem();
  23. }
  24. }
  25. });
  26. }
  27. @Override
  28. public void onPageSelected(int position) {
  29. pauseVideoPlayer();
  30. String filePath = allFiles.get(position);
  31. String fileType = functions.getmInstance(cntxt).checkFileType(filePath);
  32. if(fileType == "video") {
  33. videoView = null;
  34. startVideoPlayer(position);
  35. }
  36. }
  37. @Override
  38. public void onPageScrollStateChanged(int state) {
  39. }
  40. });
  41. //Method to implement the viewPager.setCurrentItem(filePosition)
  42. private void setViewPagerCurrentItem() {
  43. viewPager.setCurrentItem(filePosition);
  44. }
  45. private void pauseVideoPlayer() {
  46. if(!(videoView == null)){
  47. videoView.pause();
  48. }
  49. }
  50. public static void startVideoPlayer(int position) {
  51. View videoLayout = viewPager.getChildAt(position);
  52. videoView = (VideoView) videoLayout.findViewById(R.id.videoPlayer);
  53. if(!(videoView == null)) {
  54. videoView.requestFocus();
  55. videoView.start();
  56. }
  57. }

huangapple
  • 本文由 发表于 2020年4月7日 19:40:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/61079248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定