Skip to content

Commit

Permalink
Publish photos shot from Kontalk to the MediaStore (#1291)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <daniele@casaricci.it>
  • Loading branch information
daniele-athome committed Apr 2, 2020
1 parent ead07cb commit dc6c3aa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
13 changes: 8 additions & 5 deletions app/src/main/java/org/kontalk/ui/AbstractComposeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -1528,12 +1528,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
// returning from camera
if (requestCode == SELECT_ATTACHMENT_PHOTO) {
if (mCurrentPhoto != null) {
// TODO move this elsewhere and remember about scoped storage
Uri uri = Uri.fromFile(mCurrentPhoto);
// notify media scanner
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
getActivity().sendBroadcast(mediaScanIntent);
try {
MediaStorage.publishImage(getContext(), mCurrentPhoto, true);
}
catch (Exception e) {
Toast.makeText(getContext(),
// TODO i18n
"Unable to save your photo to the gallery.", Toast.LENGTH_LONG).show();
}
mCurrentPhoto = null;

uris = new Uri[]{uri};
Expand Down
77 changes: 76 additions & 1 deletion app/src/main/java/org/kontalk/util/MediaStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.kontalk.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -31,6 +33,7 @@

import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
Expand Down Expand Up @@ -88,6 +91,12 @@ public abstract class MediaStorage {
private static final String DCIM_ROOT_TYPE = Environment.DIRECTORY_DCIM;
private static final String DCIM_ROOT = "";

private static final File DCIM_PUBLIC_ROOT = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"Kontalk");
private static final String DCIM_PUBLIC_RELATIVE_PATH = new File
(Environment.DIRECTORY_DCIM, "Kontalk").toString();

private static final File DOWNLOADS_ROOT = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"Kontalk");
Expand Down Expand Up @@ -411,7 +420,13 @@ public static File getOutgoingPhotoFile(Context context) throws IOException {
}

private static File getOutgoingPhotoFile(Context context, Date date) throws IOException {
File path = new File(context.getExternalFilesDir(DCIM_ROOT_TYPE), DCIM_ROOT);
File path;
if (SystemUtils.supportsScopedStorage()) {
path = new File(context.getExternalFilesDir(DCIM_ROOT_TYPE), DCIM_ROOT);
}
else {
path = DCIM_PUBLIC_ROOT;
}
createDirectories(path);
return createImageFile(path, date);
}
Expand Down Expand Up @@ -541,6 +556,66 @@ public static String getType(String url) {
return mime;
}

/**
* Publishes some media to the {@link MediaStore}.
*/
public static void publishImage(Context context, File file, boolean photo) throws IOException {
Uri uri = Uri.fromFile(file);
if (SystemUtils.supportsScopedStorage()) {
// publish to the media store
ContentResolver resolver = context.getContentResolver();

Uri imageCollection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, uri.getLastPathSegment());
values.put(MediaStore.Images.Media.IS_PENDING, 1);
if (photo) {
values.put(MediaStore.Images.Media.RELATIVE_PATH, DCIM_PUBLIC_RELATIVE_PATH);
}
// TODO other attributes maybe?

Uri imageFile = resolver.insert(imageCollection, values);
if (imageFile == null) {
throw new FileNotFoundException("Unable to create media");
}

OutputStream imageOut = null;
InputStream imageIn = null;
try {
imageOut = resolver.openOutputStream(imageFile);
if (imageOut == null) {
throw new FileNotFoundException("Unable to create media");
}

imageIn = new FileInputStream(file);
SystemUtils.copy(imageIn, imageOut);

values.clear();
values.put(MediaStore.Images.Media.IS_PENDING, 0);
context.getContentResolver().update(imageFile, values, null, null);
}
catch (RuntimeException e) {
// something went wrong, delete dangling media
try {
resolver.delete(imageFile, null, null);
}
catch (Exception ignored) {
}
throw e;
}
finally {
SystemUtils.close(imageIn);
SystemUtils.close(imageOut);
}
}
else {
// notify media scanner
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
context.sendBroadcast(mediaScanIntent);
}
}

public static File resizeImage(Context context, Uri uri, int maxSize) throws IOException {
return resizeImage(context, uri, maxSize, maxSize, COMPRESSION_QUALITY);
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/kontalk/util/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,8 @@ public static boolean isReceivingNetworkStateChanges() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.N;
}

public static boolean supportsScopedStorage() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
}

}

0 comments on commit dc6c3aa

Please sign in to comment.