Android: add methods to get the FileDescriptor for a Uri

This can be useful for some cases when the Android APIs have calls
that expects a FileDescriptor instead of a Uri or an int file
descriptor (like a case in Qt Multimedia with MediaRecorder).

Task-number: QTBUG-96081
Task-number: QTBUG-96957
Change-Id: I0ab8d37a43b7cb94f6ebb5d48014e5a7903aadc7
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
Reviewed-by: Rami Potinkara <rami.potinkara@qt.io>
(cherry picked from commit f1c2b6f5f97cd65f70cae2d38b2e1fb19ab8502c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Assam Boudjelthia 2021-11-02 17:53:32 +02:00 committed by Qt Cherry-pick Bot
parent b45eaeafda
commit 7d6f5e8f10

View File

@ -41,6 +41,7 @@
package org.qtproject.qt.android;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Objects;
@ -240,6 +241,37 @@ public class QtNative
}
}
public static ParcelFileDescriptor openParcelFdForContentUrl(Context context, String contentUrl,
String openMode)
{
Uri uri = m_cachedUris.get(contentUrl);
if (uri == null)
uri = getUriWithValidPermission(context, contentUrl, openMode);
if (uri == null) {
Log.e(QtTAG, getCurrentMethodNameLog() + INVALID_OR_NULL_URI_ERROR_MESSAGE);
return null;
}
try {
final ContentResolver resolver = context.getContentResolver();
return resolver.openFileDescriptor(uri, openMode);
} catch (FileNotFoundException | IllegalArgumentException | SecurityException e) {
Log.e(QtTAG, getCurrentMethodNameLog() + e.toString());
}
return null;
}
public static FileDescriptor openFdObjectForContentUrl(Context context, String contentUrl,
String openMode)
{
final ParcelFileDescriptor pfd = openParcelFdForContentUrl(context, contentUrl, openMode);
if (pfd != null)
return pfd.getFileDescriptor();
return null;
}
public static int openFdForContentUrl(Context context, String contentUrl, String openMode)
{
Uri uri = m_cachedUris.get(contentUrl);