从Google相册应用中获取视频(非本地)

问题描述 投票:5回答:1

使用Google相册应用,我正在尝试选择未在设备上缓存的视频。

我正在使用ACTION_GET_CONTENT意图来启动选项对话框,并从那里选择Google相册应用。

在选择本地视频时,它会以此形式返回Uri。

内容://媒体/外部/视频/媒体/ 6708

然后,我查询内容提供程序以检索实际的文件位置,然后从那里继续。文件位置如下所示。

/ storage / emulated / 0 / WhatsApp / Media / WhatsApp Video / VID-20131102-WA0000.mp4

现在,当我选择在线视频时,即:我的设备上尚未提供的视频,以及需要下载以供使用的视频,返回的Uri如下所示:

内容://com.google.android.apps.photos.content/1/https://lh6.googleusercontent.com/_RD-QTO_SK5jlaPldTe2n5GANqMc3h-ukcbNoFlF1NLy=s0-d

现在,有了这个,没有记录的ContentProvider可以帮助我获得这个视频的实际链接。即使我进行查询,它也不会返回DISPLAY_NAME和SIZE列。

DISPLAY_NAME包含video.mpeg(不同视频的显示名称相同)

SIZE可能会告诉我实际文件的大小。

Referred to this post on SO.

我检查了各种帖子,并认为我必须通过内容提供商获取视频的InputStream,保存文件,并使用该文件。然而,选择图像文件可以正常工作,但对于视频则不然。

因此,要将流复制到文件,我有这个代码。

InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(path));

最后写入临时文件。该文件已创建,但似乎没有正确格式化。 VLC播放文件,但始终只显示第一帧。

如果我从上面给出的URI的最后一部分获取URL,并尝试在浏览器上查看它,它会下载一个GIF文件。我猜这是问题所在。但我不知道如何获得视频的mpeg格式。

有谁经历过同样的经历?

android android-intent google-plus android-photos google-photos
1个回答
8
投票

终于找到了问题的解决方案。这适用于图像和视频。

参考此视频:

DevBytes:Android 4.4存储访问框架:客户端

https://www.youtube.com/watch?v=UFj9AEz0DHQ

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivity(intent);

获取Uri,并访问并保存文件,如下所示:

ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver()
               .openFileDescriptor(Uri.parse(path),"r");

FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

InputStream inputStream = new FileInputStream(fileDescriptor);

BufferedInputStream reader = new BufferedInputStream(inputStream);

// Create an output stream to a file that you want to save to
BufferedOutputStream outStream = new BufferedOutputStream(
                    new FileOutputStream(filePath));
byte[] buf = new byte[2048];
int len;
while ((len = reader.read(buf)) > 0) {
    outStream.write(buf, 0, len);
}

出于某种原因,在不使用ParcelFileDescriptor的情况下获取输入流不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.