从Google相册应用获取视频(非本地) [英] Getting videos(non-local) from the Google Photos App

查看:214
本文介绍了从Google相册应用获取视频(非本地)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With the Google Photos app, I am trying to pick a video, that is not cached on the device.

我正在使用 ACTION_GET_CONTENT 意向来启动选项对话框,然后从中选择Google相册应用.

I am using the ACTION_GET_CONTENT intent, to launch the options dialog, and from there I choose the Google Photos app.

在选择本地视频时,它会以这种形式返回Uri.

While selecting local videos, it returns an Uri in this form.

content://media/external/video/media/6708

content://media/external/video/media/6708

然后,我查询内容提供者以检索实际文件位置,然后从那里开始.文件位置如下所示.

And from that, I query the content provider to retrieve the actual file location, and proceed from there. The file location looks like this.

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

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

现在,当我选择在线视频时,即:我的设备上尚不可用并且需要下载才能使用的视频,返回的Uri如下所示:

Now, when I choose an online video, i.e: a video not available on my device yet, and which needs to be downloaded to be used, the returned Uri looks like this:

content://com.google.android.apps.photos.content/1/现在,没有记录的ContentProvider可以帮助我获得此视频的实际链接.即使我进行查询,它也不会返回任何内容,除了DISPLAY_NAME和SIZE列.

Now, with this, there is no documented ContentProvider that would help me to get the actual link to this video. Even if I do a query, it returns nothing apart from a DISPLAY_NAME and SIZE columns.

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

DISPLAY_NAME contains video.mpeg (Same display name for different videos)

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

SIZE probably tells me the size of the actual file.

已在SO上引用了此帖子.

我检查了各种帖子,并认为我必须通过内容提供者获取视频的InputStream,保存文件并使用该文件.但是,选择一个图像文件可以很好地工作,但是对于视频来说则不行.

I checked various posts, and thought that I would have to get an InputStream for the video through the content provider, save the file, and work with the file. Picking an image file however works fine, but with video it doesn't.

因此,要将流复制到文件中,我有以下代码.

So, to copy the stream to a file, I have this code.


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

最后写一个临时文件.文件已创建,但似乎格式不正确. VLC播放文件,但始终仅显示第一帧.

And finally write to a temporary file. The file gets created, but that doesn't seem to be correctly formatted. VLC plays the file, but shows only the first frame all throughout.

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

If I take the URL from the last part of the URI given above, and try to view it on a browser, it downloads a GIF file. I am guessing that's the problem. But I don't know how to get the mpeg format of the video.

有人经历过吗?

推荐答案

最终找到了解决问题的方法.这将同时适用于图像和视频.

Finally found the solution to the problem. This would work for both images and videos.

引用了此视频:

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,然后访问并保存文件,如下所示:

Get the Uri, and access and save the file like this:


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的情况下获取输入流不起作用.

For some reason, getting an input stream without using a ParcelFileDescriptor doesn't work.

这篇关于从Google相册应用获取视频(非本地)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆