Skip to content

Commit

Permalink
Copy photos if move does not work
Browse files Browse the repository at this point in the history
If the gallery is on an external SD card, we might not have write access
and therefore don't have permissions to move the files from there.
In this case, fallback to copying.
  • Loading branch information
m-kuhn committed Jul 22, 2016
1 parent 3918f3c commit 321720b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
16 changes: 10 additions & 6 deletions src/androidpicturesource.cpp
Expand Up @@ -14,6 +14,7 @@
* *
***************************************************************************/
#include "androidpicturesource.h"
#include "qgsmessagelog.h"
#include <QAndroidJniEnvironment>
#include <QtAndroid>
#include <QDir>
Expand Down Expand Up @@ -45,21 +46,24 @@ void AndroidPictureSource::handleActivityResult( int receiverRequestCode, int re
cursor.callMethod<jboolean>( "moveToFirst", "()Z" );
QAndroidJniObject filePath = cursor.callObjectMethod( "getString", "(I)Ljava/lang/String;", columnIndex );

QDir dir( mPrefix );
if ( !dir.exists() )
if ( !QDir::root().mkpath( mPrefix ) )
{
qDebug() << "Creating folder " << mPrefix;
QDir::root().mkpath( mPrefix );
QgsMessageLog::instance()->logMessage( tr( "Could not create folder %1" ).arg( mPrefix ), "QField", QgsMessageLog::CRITICAL );
return;
}

qDebug() << "Folder " << mPrefix << " exists: " << dir.exists();
QDir dir( mPrefix );

QFileInfo fileInfo( filePath.toString() );
QString filename( fileInfo.fileName() );

if ( !QFile( filePath.toString() ).rename( dir.absoluteFilePath( filename ) ) )
{
qDebug() << "Couldn't rename file!";
qDebug() << "Couldn't rename file! Trying to copy instead";
if ( !QFile( filePath.toString() ).copy( dir.absoluteFilePath( filename ) ) )
{
QgsMessageLog::instance()->logMessage( tr( "Image %1 could not be copied to project folder %2.", "QField", QgsMessageLog::CRITICAL ).arg( filePath.toString(), mPrefix ) );
}
}

emit pictureReceived( filename );
Expand Down
16 changes: 0 additions & 16 deletions src/androidplatformutilities.cpp
Expand Up @@ -59,22 +59,6 @@ QString AndroidPlatformUtilities::getIntentExtra( QString extra, QAndroidJniObje
return "";
}


QMap<QString, QString> AndroidPlatformUtilities::getIntentExtras( QStringList intentExtras ) const
{
QAndroidJniObject extras = getNativeExtras();
QString extraValue, extraName;
QMap<QString, QString> extraMap;

for ( int i = 0; i < intentExtras.size(); ++i )
{
extraName = intentExtras.at( i ).toLocal8Bit().constData();
extraValue = getIntentExtra( extraValue, extras );
extraMap.insert( extraName, extraValue );
}
return extraMap;
}

QAndroidJniObject AndroidPlatformUtilities::getNativeIntent() const
{
QAndroidJniObject activity = QtAndroid::androidActivity();
Expand Down
3 changes: 1 addition & 2 deletions src/androidplatformutilities.h
Expand Up @@ -33,8 +33,7 @@ class AndroidPlatformUtilities : public PlatformUtilities
virtual void open( const QString& data, const QString& type );

private:
QMap<QString, QString> getIntentExtras( QStringList ) const;
QString getIntentExtra( QString, QAndroidJniObject=0 ) const;
QString getIntentExtra( QString, QAndroidJniObject = 0 ) const;
QAndroidJniObject getNativeIntent() const;
QAndroidJniObject getNativeExtras() const;
};
Expand Down

0 comments on commit 321720b

Please sign in to comment.