Skip to content

Commit

Permalink
Minor change in sharing tool
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsinSoft committed Apr 17, 2024
1 parent f0a6bd1 commit ab32f65
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
6 changes: 4 additions & 2 deletions QtAndroidTools/QAndroidSharing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/
#include <QFile>
#include <QStandardPaths>
#include <QCoreApplication>
#include "QAndroidSharing.h"

Expand Down Expand Up @@ -172,8 +173,9 @@ bool QAndroidSharing::saveRequestedSharedFile(const QString &filePath)

if(sharedFileDataObj.isValid())
{
const QByteArray sharedFileData = convertByteArray(sharedFileDataObj);
QFile sharedFile(filePath);
const QString filesDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/");
const QByteArray sharedFileData(convertByteArray(sharedFileDataObj));
QFile sharedFile(filesDir + filePath);

if(sharedFile.open(QIODevice::WriteOnly) == true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,15 @@ public boolean shareText(String text)
public boolean shareBinaryData(String mimeType, String dataFilePath)
{
final String packageName = mActivityInstance.getApplicationContext().getPackageName();
final String filesDir = (mActivityInstance.getFilesDir().getAbsolutePath() + "/");
final Intent sendIntent = new Intent();
Uri fileUri;

try
{
fileUri = FileProvider.getUriForFile(mActivityInstance,
packageName + ".qtprovider",
new File(dataFilePath)
new File(filesDir + dataFilePath)
);
}
catch(IllegalArgumentException e)
Expand All @@ -165,6 +166,7 @@ public boolean shareBinaryData(String mimeType, String dataFilePath)
public boolean shareFile(boolean fileAvailable, String mimeType, String filePath)
{
final String packageName = mActivityInstance.getApplicationContext().getPackageName();
final String filesDir = (mActivityInstance.getFilesDir().getAbsolutePath() + "/");
final Intent returnFileIntent = new Intent(packageName + ".ACTION_RETURN_FILE");

if(fileAvailable == true)
Expand All @@ -175,7 +177,7 @@ public boolean shareFile(boolean fileAvailable, String mimeType, String filePath
{
fileUri = FileProvider.getUriForFile(mActivityInstance,
packageName + ".qtprovider",
new File(filePath)
new File(filesDir + filePath)
);
}
catch(IllegalArgumentException e)
Expand Down
49 changes: 34 additions & 15 deletions QtAndroidToolsDemo/tools/AndroidSharing.qml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import QtCore
import QtQuick
import QtQuick.Controls.Material
import QtQuick.Dialogs
import QtAndroidTools
import Qt.labs.platform as Platform

Page {
id: page
Expand Down Expand Up @@ -32,7 +33,7 @@ Page {
target: QtAndroidSharing
function onRequestedSharedFileReadyToSave(mimeType, name, size)
{
requestedSharedFile.text = "Name: " + name + "\nSize: " + size + "\nMimeType: " + mimeType;
requestedSharedFile.text = ("Name: " + name + "\nSize: " + size + "\nMimeType: " + mimeType);
requestedSharedFile.fileName = name;
requestedSharedFile.open();
}
Expand All @@ -53,7 +54,7 @@ Page {
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Share binary data"
onClicked: QtAndroidSharing.shareBinaryData("image/jpeg", QtAndroidSystem.dataLocation + "/sharedfiles/logo_falsinsoft.jpg")
onClicked: QtAndroidSharing.shareBinaryData("image/jpeg", "sharedfiles/logo_falsinsoft.jpg")
}

Button {
Expand All @@ -63,7 +64,7 @@ Page {
}
}

Platform.MessageDialog {
MessageDialog {
id: receivedSharedText
title: "Received shared text"
onAccepted: Qt.quit()
Expand All @@ -89,17 +90,29 @@ Page {
onAccepted: if(quitOnClose) Qt.quit()
}

Platform.MessageDialog {
MessageDialog {
id: requestedSharedFile
title: "It's ok to get this file?"
buttons: Platform.MessageDialog.Yes | Platform.MessageDialog.No
onRejected: QtAndroidSharing.closeRequestedSharedFile()
onAccepted: {
var filePath = QtAndroidSystem.dataLocation + "/sharedfiles/" + fileName;
QtAndroidSharing.saveRequestedSharedFile(filePath);
sharedImage.source = "file:/" + filePath;
receivedSharedImage.quitOnClose = false;
receivedSharedImage.open();
buttons: MessageDialog.Yes | MessageDialog.No
onButtonClicked: function (button, role) {
if(button === MessageDialog.Yes)
{
if(QtAndroidSharing.saveRequestedSharedFile("sharedfiles/" + fileName))
{
sharedImage.source = (StandardPaths.writableLocation(StandardPaths.AppDataLocation) + "/sharedfiles/" + fileName);
receivedSharedImage.quitOnClose = false;
receivedSharedImage.open();
}
else
{
errorMessage.text = "Unable to save received file";
errorMessage.open();
}
}
else
{
QtAndroidSharing.closeRequestedSharedFile();
}
}
property string fileName
}
Expand All @@ -117,16 +130,22 @@ Page {
id: imageToShare
width: page.width * 0.5
height: width
source: "file:/" + QtAndroidSystem.dataLocation + "/sharedfiles/logo_falsinsoft.jpg"
source: StandardPaths.writableLocation(StandardPaths.AppDataLocation) + "/sharedfiles/logo_falsinsoft.jpg"
}

onRejected: {
QtAndroidSharing.shareFile(false);
Qt.quit();
}
onAccepted: {
QtAndroidSharing.shareFile(true, "image/jpeg", QtAndroidSystem.dataLocation + "/sharedfiles/logo_falsinsoft.jpg");
QtAndroidSharing.shareFile(true, "image/jpeg", "sharedfiles/logo_falsinsoft.jpg");
Qt.quit();
}
}

MessageDialog {
id: errorMessage
title: "Error"
buttons: MessageDialog.Ok
}
}

0 comments on commit ab32f65

Please sign in to comment.