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

和自甴很熟 提交于 2019-12-03 15:11:51

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!