Skip to content

Commit

Permalink
Bring camera pictures in app-specific external files directory (#1291)
Browse files Browse the repository at this point in the history
This will keep a copy of pictures taken with the camera app inside an
app-specific folder that can't be reached by an average user (you'll
need a file manager). In the future those pictures should be contributed
to the MediaStore.

Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Mar 30, 2020
1 parent 362f513 commit f4d20fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ void startCameraAttachment() {
return;

try {
mCurrentPhoto = MediaStorage.getOutgoingPhotoFile();
mCurrentPhoto = MediaStorage.getOutgoingPhotoFile(context);

final Intent intent = SystemUtils.externalIntent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = MediaStorage.getWorldWritableUri(getContext(),
Expand Down Expand Up @@ -1528,6 +1528,7 @@ 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);
Expand Down
33 changes: 18 additions & 15 deletions app/src/main/java/org/kontalk/util/MediaStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public abstract class MediaStorage {
private static final String PICTURES_ROOT = "";
private static final String PICTURES_SENT_ROOT = "Sent";

private static final File DCIM_ROOT = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"Kontalk");
private static final String DCIM_ROOT_TYPE = Environment.DIRECTORY_DCIM;
private static final String DCIM_ROOT = "";

private static final File DOWNLOADS_ROOT = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"Kontalk");
Expand Down Expand Up @@ -406,12 +406,14 @@ public void consume() throws IOException {
}

/** Creates a temporary JPEG file for a photo (DCIM). */
public static File getOutgoingPhotoFile() throws IOException {
return getOutgoingPhotoFile(new Date());
public static File getOutgoingPhotoFile(Context context) throws IOException {
return getOutgoingPhotoFile(context, new Date());
}

private static File getOutgoingPhotoFile(Date date) throws IOException {
return createImageFile(DCIM_ROOT, date);
private static File getOutgoingPhotoFile(Context context, Date date) throws IOException {
File path = new File(context.getExternalFilesDir(DCIM_ROOT_TYPE), DCIM_ROOT);
createDirectories(path);
return createImageFile(path, date);
}

/** Creates a temporary JPEG file for a picture (Pictures). */
Expand All @@ -421,12 +423,12 @@ public static File getOutgoingPictureFile(Context context) throws IOException {

private static File getOutgoingPictureFile(Context context, Date date) throws IOException {
File path = new File(context.getExternalFilesDir(PICTURES_ROOT_TYPE), PICTURES_SENT_ROOT);
createNoMedia(path);
createDirectories(path);
return createImageFile(path, date);
}

private static File createImageFile(File path, Date date) throws IOException {
createMedia(path);
createDirectories(path);
String timeStamp = sDateFormat.format(date);
File f = new File(path, "IMG_" + timeStamp + ".jpg");
f.createNewFile();
Expand All @@ -441,7 +443,7 @@ public static String getOutgoingPictureFilename(Date date, String extension) {
/** Creates a file object for an incoming image file. */
public static File getIncomingImageFile(Context context, Date date, String extension) {
File path = new File(context.getExternalFilesDir(PICTURES_ROOT_TYPE), PICTURES_ROOT);
createMedia(path);
createDirectories(path);
String timeStamp = sDateFormat.format(date);
return new File(path, "IMG_" + timeStamp + "." + extension);
}
Expand All @@ -453,7 +455,7 @@ public static File getOutgoingAudioFile(Context context) throws IOException {

private static File getOutgoingAudioFile(Context context, Date date) throws IOException {
File path = new File(context.getExternalFilesDir(RECORDING_ROOT_TYPE), RECORDING_SENT_ROOT);
createNoMedia(path);
createDirectories(path);
String timeStamp = sDateFormat.format(date);
File f = new File(path, "record_" + timeStamp + "." + AudioRecording.FILE_EXTENSION);
f.createNewFile();
Expand All @@ -468,26 +470,27 @@ public static String getOutgoingAudioFilename(Date date, String extension) {
/** Creates a file object for an incoming audio file. */
public static File getIncomingAudioFile(Context context, Date date, String extension) {
File path = new File(context.getExternalFilesDir(RECORDING_ROOT_TYPE), RECORDING_ROOT);
createNoMedia(path);
createDirectories(path);
String timeStamp = sDateFormat.format(date);
return new File(path, "audio_" + timeStamp + "." + extension);
}

public static File getIncomingFile(Date date, String extension) {
createMedia(DOWNLOADS_ROOT);
createNoMedia(DOWNLOADS_ROOT);
String timeStamp = sDateFormat.format(date);
return new File(DOWNLOADS_ROOT, "file_" + timeStamp + "." + extension);
}

/** Ensures that the given path exists. */
private static boolean createMedia(File path) {
private static boolean createDirectories(File path) {
return path.isDirectory() || path.mkdirs();
}

/** Ensures that the given path exists and a .nomedia file exists. */
@Deprecated
private static boolean createNoMedia(File path) {
try {
if (createMedia(path)) {
if (createDirectories(path)) {
File nomedia = new File(path, ".nomedia");
return nomedia.isFile() || nomedia.createNewFile();
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

<external-files-path name="kontalk_recordings" path="Recordings" />
<external-files-path name="kontalk_pictures" path="Pictures" />
<external-path name="kontalk_downloads" path="Download/Kontalk" />
<external-path name="kontalk_photos" path="DCIM/Kontalk" />
<external-files-path name="kontalk_photos" path="DCIM/Kontalk" />

<!-- FIXME won't work on API level 29 -->
<external-files-path name="kontalk_downloads" path="Download/Kontalk" />

<cache-path name="kontalk_debug" path="debug" />
</paths>

0 comments on commit f4d20fc

Please sign in to comment.