Skip to content

Commit

Permalink
Make sure the full path of a taken photo / image is created on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jan 22, 2023
1 parent d302fdf commit fc58ddf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
Expand Up @@ -103,13 +103,8 @@ protected void onActivityResult(int requestCode, int resultCode,
if (requestCode == CAMERA_ACTIVITY) {
if (resultCode == RESULT_OK) {
File result = new File(prefix + pictureFilePath);
File path = result.getParentFile();
path.mkdirs();
path.setExecutable(true);
path.setReadable(true);
path.setWritable(true);

File pictureFile = new File(pictureTempFilePath);

Log.d(TAG, "Taken picture: " + pictureFile.getAbsolutePath());
try {
InputStream in = new FileInputStream(pictureFile);
Expand All @@ -119,13 +114,6 @@ protected void onActivityResult(int requestCode, int resultCode,
e.printStackTrace();
}

// Let the android scan new media folders/files to make them
// visible through MTP
result.setReadable(true);
result.setWritable(true);
MediaScannerConnection.scanFile(
this, new String[] {path.toString()}, null, null);

Intent intent = this.getIntent();
intent.putExtra("PICTURE_IMAGE_FILENAME",
prefix + pictureFilePath);
Expand Down
Expand Up @@ -67,11 +67,6 @@ protected void onActivityResult(int requestCode, int resultCode,
if (requestCode == GALLERY_ACTIVITY) {
if (resultCode == RESULT_OK) {
File result = new File(prefix + pictureFilePath);
File path = result.getParentFile();
path.mkdirs();
path.setExecutable(true);
path.setReadable(true);
path.setWritable(true);

Log.d(TAG, "Selected picture: " + data.getData().toString());
try {
Expand All @@ -85,13 +80,6 @@ protected void onActivityResult(int requestCode, int resultCode,
Log.d(TAG, e.getMessage());
}

// Let the android scan new media folders/files to make them
// visible through MTP
result.setReadable(true);
result.setWritable(true);
MediaScannerConnection.scanFile(
this, new String[] {path.toString()}, null, null);

Intent intent = this.getIntent();
intent.putExtra("PICTURE_IMAGE_FILENAME",
prefix + pictureFilePath);
Expand Down
9 changes: 9 additions & 0 deletions src/core/platforms/android/androidplatformutilities.cpp
Expand Up @@ -30,6 +30,7 @@
#include <QAndroidJniObject>
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMap>
Expand Down Expand Up @@ -345,6 +346,10 @@ PictureSource *AndroidPlatformUtilities::getCameraPicture( QQuickItem *parent, c
if ( !checkCameraPermissions() )
return nullptr;

const QFileInfo destinationInfo( prefix + pictureFilePath );
const QDir prefixDir( prefix );
prefixDir.mkpath( destinationInfo.absolutePath() );

QAndroidJniObject activity = QAndroidJniObject::fromString( QStringLiteral( "ch.opengis." APP_PACKAGE_NAME ".QFieldCameraPictureActivity" ) );
QAndroidJniObject intent = QAndroidJniObject( "android/content/Intent", "(Ljava/lang/String;)V", activity.object<jstring>() );
QAndroidJniObject packageName = QAndroidJniObject::fromString( QStringLiteral( "ch.opengis." APP_PACKAGE_NAME ) );
Expand Down Expand Up @@ -384,6 +389,10 @@ PictureSource *AndroidPlatformUtilities::getGalleryPicture( QQuickItem *parent,
{
Q_UNUSED( parent )

const QFileInfo destinationInfo( prefix + pictureFilePath );
const QDir prefixDir( prefix );
prefixDir.mkpath( destinationInfo.absolutePath() );

QAndroidJniObject activity = QAndroidJniObject::fromString( QStringLiteral( "ch.opengis." APP_PACKAGE_NAME ".QFieldGalleryPictureActivity" ) );
QAndroidJniObject intent = QAndroidJniObject( "android/content/Intent", "(Ljava/lang/String;)V", activity.object<jstring>() );
QAndroidJniObject packageName = QAndroidJniObject::fromString( QStringLiteral( "ch.opengis." APP_PACKAGE_NAME ) );
Expand Down

1 comment on commit fc58ddf

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.