Skip to content

Commit

Permalink
Added param to set the format to use for image save
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsinSoft committed Feb 9, 2024
1 parent dfaa3c0 commit a76cbd9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ <h2>Images</h2>
<pre><code class="cpp">QtAndroidImages.addImageToGallery(imagePath)</code></pre>
<p>The image is not moved from original location but it became part of the system gallery and is showed in the gallery app.</p>
<p>On the contrary the following function:</p>
<pre><code class="cpp">QtAndroidImages.saveImageToGallery(name, image)</code></pre>
<p>save the image into the system gallery.</p>
<pre><code class="cpp">QtAndroidImages.saveImageToGallery(name, image, format)</code></pre>
<p>save the image into the system gallery. With the param <i>format</i> is possible to set the image format to use and can be QAndroidImages.FORMAT_JPEG or QAndroidImages.FORMAT_PNG.</p>
</section>

<hr class="divider">
Expand Down
7 changes: 4 additions & 3 deletions QtAndroidTools/QAndroidImages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ void QAndroidImages::addImageToGallery(const QString &imagePath)
}
}

bool QAndroidImages::saveImageToGallery(const QString &name, const QImage &image)
bool QAndroidImages::saveImageToGallery(const QString &name, const QImage &image, IMAGE_FORMAT format)
{
if(m_javaImages.isValid())
{
const QJniObject androidBitmap = QtAndroidTools::imageToAndroidBitmap(image);

return m_javaImages.callMethod<jboolean>("saveImageToGallery",
"(Ljava/lang/String;Landroid/graphics/Bitmap;)Z",
"(Ljava/lang/String;Landroid/graphics/Bitmap;I)Z",
QJniObject::fromString(name).object<jstring>(),
androidBitmap.object()
androidBitmap.object(),
format
);
}

Expand Down
9 changes: 8 additions & 1 deletion QtAndroidTools/QAndroidImages.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ class QAndroidImages : public QObject
public:
QAndroidImages(QObject *parent);

enum IMAGE_FORMAT
{
FORMAT_JPEG = 0,
FORMAT_PNG
};
Q_ENUM(IMAGE_FORMAT)

static QAndroidImages* create(QQmlEngine *engine, QJSEngine *scriptEngine);
static QAndroidImages* instance();

Q_INVOKABLE QStringList getAlbumsList();
Q_INVOKABLE QStringList getAlbumImagesList(const QString &name);
Q_INVOKABLE void addImageToGallery(const QString &imagePath);
Q_INVOKABLE bool saveImageToGallery(const QString &name, const QImage &image);
Q_INVOKABLE bool saveImageToGallery(const QString &name, const QImage &image, IMAGE_FORMAT format);
Q_INVOKABLE bool imageFileExist(const QString &name);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,37 @@ public void addImageToGallery(String imagePath)
mActivityInstance.sendBroadcast(mediaScanIntent);
}

public boolean saveImageToGallery(String name, Bitmap bitmap)
public boolean saveImageToGallery(String name, Bitmap bitmap, int format)
{
final Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Bitmap.CompressFormat compressFormat;
OutputStream imageOutStream;
String fileExtension;
Uri imageUri;

switch(format)
{
case FORMAT_JPEG:
compressFormat = Bitmap.CompressFormat.JPEG;
fileExtension = "jpeg";
break;
case FORMAT_PNG:
compressFormat = Bitmap.CompressFormat.PNG;
fileExtension = "png";
break;
default:
return false;
}

try
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
final ContentResolver resolver = mActivityInstance.getContentResolver();
final ContentValues values = new ContentValues();

values.put(MediaStore.Images.Media.DISPLAY_NAME, name + ".jpg");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DISPLAY_NAME, name + "." + fileExtension);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/" + fileExtension);
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);

imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Expand All @@ -159,13 +175,13 @@ public boolean saveImageToGallery(String name, Bitmap bitmap)
else
{
final String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
final File image = new File(imagesDir, name + ".jpg");
final File image = new File(imagesDir, name + "." + fileExtension);

imageUri = Uri.fromFile(image);
imageOutStream = new FileOutputStream(image);
}

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, imageOutStream);
bitmap.compress(compressFormat, 100, imageOutStream);
imageOutStream.close();
}
catch(IOException e)
Expand All @@ -186,4 +202,7 @@ public boolean imageFileExist(String name)

return image.isFile();
}

private static final int FORMAT_JPEG = 0;
private static final int FORMAT_PNG = 1;
}

0 comments on commit a76cbd9

Please sign in to comment.