Getting videos(non-local) from the Google Photos App
Asked Answered
H

1

6

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

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

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

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 Video/VID-20131102-WA0000.mp4

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/https://lh6.googleusercontent.com/_RD-QTO_SK5jlaPldTe2n5GANqMc3h-ukcbNoFlF1NLy=s0-d

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 contains video.mpeg (Same display name for different videos)

SIZE probably tells me the size of the actual file.

Referred to this post on SO.

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));

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.

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.

Anyone has experienced the same?

Hilmahilt answered 13/12, 2013 at 4:59 Comment(0)
H
10

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

Referenced this video:

DevBytes: Android 4.4 Storage Access Framework: Client

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

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

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);
}

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

Hilmahilt answered 8/1, 2014 at 9:59 Comment(2)
Massive thank you for this, this has helped alot! Here is a related question and accepted answer: https://mcmap.net/q/541462/-using-data-from-context-providers-or-requesting-google-photos-read-permission/3309883Roller
outStream.flush(); at the end would be helpful though.Byng

© 2022 - 2024 — McMap. All rights reserved.