微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Android 10 及更高版本中获取共享存储文件夹的文件路径?

如何解决如何在 Android 10 及更高版本中获取共享存储文件夹的文件路径?

我们正在我们的应用程序中集成范围存储,我们正在 sahred 存储中读取和写入视频文件,例如 Environment.DIRECTORY_MOVIES 文件夹(共享存储电影目录)它按预期工作,但我们需要将视频中的自定义元数据写入这样做我们正在使用org.mp4parser:isoparser。为了读写元数据,这个库在范围存储之前需要文件对象,我们可以使用 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) 获取绝对路径,现在它已被弃用有没有其他方法可以在范围存储中获取文件路径?

public static String readVideoMetadata(File videoFile) throws IOException {
    
        if (!videoFile.canRead()) {
            throw new IllegalStateException("No read permissions to file " + videoFile.getAbsolutePath());
        }
        if(!isImageFile(videoFile)) {
            try {
                IsoFile isoFile = new IsoFile(videoFile);
                if (null != Path.getPath(isoFile,"moov[0]/udta[0]/Meta[0]/ilst/©cmt")) {
                    AppleCommentBox nam = Path.getPath(isoFile,"moov[0]/udta[0]/Meta[0]/ilst/©cmt");
                    String xml = nam.getValue();
                    isoFile.close();
                    return xml;
                }
            } catch (OutOfMemoryError | Exception e) {
                e.printstacktrace();
            }
        }else{
            ExifInterface exifInterface=new ExifInterface(videoFile);
            String MetaData= exifInterface.getAttribute(ExifInterface.TAG_USER_COMMENT);
            if(MetaData!=null){
                return MetaData;
            }
        }
    
        return "";
    }

解决方法

Use FFMPEG Android library

您可以使用以下代码从媒体 URI 获取文件路径

Uri safUri = intent.getData();
String inputVideoPath = FFmpegKitConfig.getSafParameterForRead(requireContext(),safUri);

您可以使用以下功能读取媒体信息

 MediaInformationSession mediaInformation = FFprobeKit.getMediaInformation("<file path or uri>");
mediaInformation.getMediaInformation();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。