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

Pick-to: 6.2
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>
bb10
Assam Boudjelthia 2021-11-02 17:53:32 +02:00
parent be55118bd3
commit f1c2b6f5f9
1 changed files with 32 additions and 0 deletions

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