I want to open a file (.xls, .pdf, .png, etc.) using QML on Android (if iOS works even better).
I'm trying to use it like this:
Qt.openUrlExternally(url)
/*Qt.openUrlExternally("http://www.example.com)"*/
/*Qt.openUrlExternally("file:///C:/Users/Hello/Pictures/Qt.jpg")*/
Both the Internet URL and the Local URL open perfectly on Windows. The Internet URL works also on Android, however I can't open a local file on Android.
This is what I've tried on Android:
Qt.openUrlExternally("file:/storage/emulated/0/Documents/test_pdf.pdf")
Qt.openUrlExternally("file:///storage/emulated/0/Documents/test_pdf.pdf")
Qt.openUrlExternally(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) "/test_pdf.pdf")
Qt.openUrlExternally("content:///storage/emulated/0/Documents/test_pdf.pdf");
I know there's a couple questios about this on here but no one answered me and also the example don't show how to open a local file.
I have Storage permission BTW and the file it's there for sure.
edit: if I pick the file through QMLs fileDilog this is the URL I get: content://com.android.externalstorage.documents/document/primary:Documents//20BLOQUES.pdf
CodePudding user response:
This is due to restriction by Android OS.
You can disable this restriction by using the following code inside of Java part of your application (it can also be written using C by the way):
import java.lang.reflect.Method;
import android.os.StrictMode;
import android.os.Build;
if (Build.VERSION.SDK_INT >= 24)
{
try
{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e)
{
e.printStackTrace();
}
}
However, it is not recommended by Google and the recommended way to open files is to use content
scheme. You can read about this in more detail here: https://www.qt.io/blog/2017/12/01/sharing-files-android-ios-qt-app.
CodePudding user response:
So, after fighting with this I got to the next conclusions and clarifications:
I can't use JAVA or C . All I can use is QML. This is a closed software platform called Velneo and I only have access to the QML side.
I can't use file:///, it has to be content:///. You can read more about this here.
Android won't open files from /storage/emulated/0/Documents/ by just typing that route. The URL has to be formatted like this: content://com.android.externalstorage.documents/document/primary:Documents/test_pdf.pdf (read #2).
It only works on Android 11, it does NOT on Android 10 for some reason.
I'll accept this as the correct answer simply because in my case it's what I needed, I get to meet my requirements.
Just posting this reply in case anyone, some day, will find themselves in the same position.