mirror of
https://github.com/QuasarApp/QtAndroidTools.git
synced 2025-04-26 13:04:32 +00:00
Added code for share file with other apps
This commit is contained in:
parent
bc7249aca5
commit
e6d0187db5
@ -190,6 +190,21 @@ void QAndroidSharing::closeSharedFile()
|
||||
}
|
||||
}
|
||||
|
||||
bool QAndroidSharing::returnSharedFile(bool FileAvailable, const QString &MimeType, const QString &FilePath)
|
||||
{
|
||||
if(m_JavaSharing.isValid())
|
||||
{
|
||||
return m_JavaSharing.callMethod<jboolean>("returnSharedFile",
|
||||
"(ZLjava/lang/String;Ljava/lang/String;)Z",
|
||||
FileAvailable,
|
||||
QAndroidJniObject::fromString(MimeType).object<jstring>(),
|
||||
QAndroidJniObject::fromString(FilePath).object<jstring>()
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void QAndroidSharing::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
|
||||
{
|
||||
Q_UNUSED(resultCode)
|
||||
|
@ -56,6 +56,7 @@ public:
|
||||
Q_INVOKABLE bool requestSharedFile(const QString &MimeType);
|
||||
Q_INVOKABLE QByteArray getRequestedSharedFile();
|
||||
Q_INVOKABLE void closeSharedFile();
|
||||
Q_INVOKABLE bool returnSharedFile(bool FileAvailable, const QString &MimeType = QString(), const QString &FilePath = QString());
|
||||
|
||||
ACTION_ID getAction() const;
|
||||
QString getMimeType() const;
|
||||
|
@ -131,17 +131,18 @@ public class AndroidSharing
|
||||
|
||||
public boolean shareData(String MimeType, String DataFilePath)
|
||||
{
|
||||
final String PackageName = mActivityInstance.getApplicationContext().getPackageName();
|
||||
Intent SendIntent = new Intent();
|
||||
Uri FileUri;
|
||||
|
||||
try
|
||||
{
|
||||
FileUri = FileProvider.getUriForFile(mActivityInstance,
|
||||
mActivityInstance.getApplicationContext().getPackageName() + ".qtandroidtoolsfileprovider",
|
||||
PackageName + ".qtandroidtoolsfileprovider",
|
||||
new File(DataFilePath)
|
||||
);
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
catch(IllegalArgumentException e)
|
||||
{
|
||||
Log.e(TAG, "The selected file can't be shared: " + DataFilePath);
|
||||
return false;
|
||||
@ -156,6 +157,41 @@ public class AndroidSharing
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean returnSharedFile(boolean FileAvailable, String MimeType, String FilePath)
|
||||
{
|
||||
final String PackageName = mActivityInstance.getApplicationContext().getPackageName();
|
||||
Intent ReturnIntent = new Intent(PackageName + ".ACTION_RETURN_FILE");
|
||||
|
||||
if(FileAvailable == true)
|
||||
{
|
||||
Uri FileUri;
|
||||
|
||||
try
|
||||
{
|
||||
FileUri = FileProvider.getUriForFile(mActivityInstance,
|
||||
PackageName + ".qtandroidtoolsfileprovider",
|
||||
new File(FilePath)
|
||||
);
|
||||
}
|
||||
catch(IllegalArgumentException e)
|
||||
{
|
||||
Log.e(TAG, "The selected file can't be shared: " + FilePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
ReturnIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
ReturnIntent.setDataAndType(FileUri, MimeType);
|
||||
mActivityInstance.setResult(Activity.RESULT_OK, ReturnIntent);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReturnIntent.setDataAndType(null, "");
|
||||
mActivityInstance.setResult(Activity.RESULT_CANCELED, ReturnIntent);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public byte[] getRequestedSharedFile()
|
||||
{
|
||||
byte[] ByteArray = null;
|
||||
|
@ -24,7 +24,7 @@ Page {
|
||||
}
|
||||
else if(QtAndroidSharing.action === QtAndroidSharing.ACTION_PICK)
|
||||
{
|
||||
|
||||
imageToShareDialog.open();
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,4 +113,30 @@ Page {
|
||||
receivedSharedImage.open();
|
||||
}
|
||||
}
|
||||
|
||||
Dialog {
|
||||
id: imageToShareDialog
|
||||
title: "Sorry, I have only this image to share,\ndo you want it?"
|
||||
modal: true
|
||||
standardButtons: Dialog.Yes | Dialog.No
|
||||
contentWidth: imageToShare.width
|
||||
contentHeight: imageToShare.height
|
||||
anchors.centerIn: parent
|
||||
|
||||
Image {
|
||||
id: imageToShare
|
||||
width: page.width * 0.5
|
||||
height: width
|
||||
source: "file:/" + QtAndroidSystem.dataLocation + "/sharedfiles/logo_falsinsoft.jpg"
|
||||
}
|
||||
|
||||
onRejected: {
|
||||
QtAndroidSharing.returnSharedFile(false);
|
||||
Qt.quit();
|
||||
}
|
||||
onAccepted: {
|
||||
QtAndroidSharing.returnSharedFile(true, "image/jpeg", QtAndroidSystem.dataLocation + "/sharedfiles/logo_falsinsoft.jpg");
|
||||
Qt.quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user