重新打开应用程序后,可以从URI播放视频

发布于 2025-01-22 06:06:57 字数 1778 浏览 0 评论 0原文

我让用户从画廊中选择一个视频,然后保存视频的URI供将来使用。但是,在我关闭应用程序并重新打开它之后,视频不再播放了 这是我用于从画廊中选择的视频使用的目的

private void pickVideoFromGallery() {
        Intent opengalleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        opengalleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
        opengalleryIntent.setType("video/*");
        opengalleryIntent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                | Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(opengalleryIntent, RESULT_LOAD_VIDEO);
    } 

,我将视频UI作为字符串

    String videoPath = data.getData().toString();

使用:

    String videoPath= getIntent().getStringExtra("videoPath"); //From PendingIntent in ForegroundService
            viFriendCall.setVideoURI(Uri.parse(videoPath));
            viFriendCall.setOnPreparedListener(mp -> mp.setLooping(true));
            viFriendCall.start();

这是错误消息,我得到的

E/MediaPlayerNative: Unable to create media player
W/VideoView: Unable to open content: content://com.android.providers.media.documents/document/video%3A9922
    java.io.IOException: setDataSource failed.: status=0x80000000
        at android.media.MediaPlayer.nativeSetDataSource(Native Method)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1209)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1196)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1107)

”

I let the user select a video from the gallery then I save the uri of the video for future use. But after I close the app and reopen it, the video can't play anymore
This is intent i use for select video from gallery

private void pickVideoFromGallery() {
        Intent opengalleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        opengalleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
        opengalleryIntent.setType("video/*");
        opengalleryIntent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                | Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(opengalleryIntent, RESULT_LOAD_VIDEO);
    } 

I save the video uri as a string

    String videoPath = data.getData().toString();

Use:

    String videoPath= getIntent().getStringExtra("videoPath"); //From PendingIntent in ForegroundService
            viFriendCall.setVideoURI(Uri.parse(videoPath));
            viFriendCall.setOnPreparedListener(mp -> mp.setLooping(true));
            viFriendCall.start();

And this is the error message i get

E/MediaPlayerNative: Unable to create media player
W/VideoView: Unable to open content: content://com.android.providers.media.documents/document/video%3A9922
    java.io.IOException: setDataSource failed.: status=0x80000000
        at android.media.MediaPlayer.nativeSetDataSource(Native Method)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1209)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1196)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1107)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2025-01-29 06:06:57

此错误出现在Android 11及更高版本上。最初承认关闭应用程序后,您的URI将不再可以访问该文件。这是Android 11版本的新更新,您可以读取它在这里

有一个简单的解决方案。如果要访问该文件以供以后在应用中使用,则该条目将其从原始位置复制到应用程序的缓存或文件目录。

您会发现很多文档。当您首先获取文件的URI时,只需将文件保存到文件目录中即可。

This error appears on devices android 11 and above. Originally admitted that after closing the app your uri no longer has access to the file.This is a new update from android 11 version, you can read it here

There's one simple solution to it. If you want to access the file for later use in your app, the entry copying it from the original location to your app's cache or files directory.

You'll find plenty of documents on it. Just save your file into the files directory when you first get the URI of your files.

鼻尖触碰 2025-01-29 06:06:57

您必须在Android中使用缓存来保存数据以备将来使用

  1. 房间数据(可以用于复杂数据,例如对象,类): https://developer.android.com/training/data-storage/room

  2. 数据存储(可用于存储原始数据类型)
    https://developer.android.com/主题/库/架构/datastore?gclsrc = aw.ds& gclid = cj0kcqjwmpssbhcnarisah3cygakcy6vvbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj7wdk8ebvtk8ebvtk8ebvtffpwsbo3ffpwsbo3funjym6funjym6funjym6uc2gkfygkfygkfygeamurealalealwcbb

初始化将从内存中清除,当用户返回时到该应用程序所有变量将从新鲜初始化

You have to use caching in Android to save your data for future use

  1. Room data (can be use for complex data such as object, classes): https://developer.android.com/training/data-storage/room

  2. Data store (can be use to store the primitive data types)
    https://developer.android.com/topic/libraries/architecture/datastore?gclsrc=aw.ds&gclid=Cj0KCQjwmPSSBhCNARIsAH3cYgaKcy6vvBjj2320H6OSog7Wdk8eBVtFrpwsBO3fuNJYm6uc2GKFYGEaAmUrEALw_wcB

And the problem with your code is when user close the app, all variable that you initialized will be cleared from the memory and when user comes back to the app all variable will be initialized from fresh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文