Skip to content

Commit

Permalink
Move SyncthingCamera folder from /Android/data to /Android/media (#809)
Browse files Browse the repository at this point in the history
* Update ConfigXml.java

* Update FileUtils.java

* Update PhotoShootActivity.java

* FileUtils: INT_MEDIA, EXT_MEDIA

* Update FileUtils.java

* Update file_paths.xml

* Update PhotoShootActivity.java

* Update FolderActivity.java

* Revert "Update FolderActivity.java"

This reverts commit 7dcc6a7.

* Update FileUtils.java

* Update FileUtils.java

* Update FileUtils.java
  • Loading branch information
Catfriend1 authored May 25, 2021
1 parent 0508903 commit 679cbd0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public Object instantiateItem(ViewGroup container, int position) {
@Override
public void onClick(View v) {
// Get app specific /Android/media directory.
File externalFilesDir = FileUtils.getExternalFilesDir(FirstStartActivity.this, ExternalStorageDirType.MEDIA, null);
File externalFilesDir = FileUtils.getExternalFilesDir(FirstStartActivity.this, ExternalStorageDirType.INT_MEDIA, null);
if (externalFilesDir == null) {
Log.w(TAG, "Failed to export config. Could not determine app's private files directory on external storage.");
Toast.makeText(FirstStartActivity.this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private void restoreDialogStates(Bundle savedInstanceState) {
@SuppressLint("InlinedAPI")
private void onPathViewClick() {
// This has to be android.net.Uri as it implements a Parcelable.
android.net.Uri externalFilesDirUri = FileUtils.getExternalFilesDirUri(FolderActivity.this, ExternalStorageDirType.MEDIA);
android.net.Uri externalFilesDirUri = FileUtils.getExternalFilesDirUri(FolderActivity.this, ExternalStorageDirType.INT_MEDIA);

// Display storage access framework directory picker UI.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.util.FileUtils;
import com.nutomic.syncthingandroid.util.FileUtils.ExternalStorageDirType;

import java.io.IOException;
import java.io.File;
Expand Down Expand Up @@ -193,11 +194,12 @@ private File createImageFile() throws IOException {
new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = FileUtils.getExternalFilesDir(this, Environment.DIRECTORY_PICTURES);
File storageDir = FileUtils.getExternalFilesDir(PhotoShootActivity.this, ExternalStorageDirType.INT_MEDIA, Environment.DIRECTORY_PICTURES);
if (storageDir == null) {
Log.e(TAG, "createImageFile: storageDir == null");
return null;
}
storageDir.mkdirs();
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.nutomic.syncthingandroid.service.AppPrefs;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.SyncthingRunnable;
import com.nutomic.syncthingandroid.util.FileUtils.ExternalStorageDirType;

import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -1104,8 +1105,8 @@ private boolean addSyncthingCameraFolder() {
return false;
}

// Get app specific directory, e.g. "/storage/emulated/0/Android/data/[PACKAGE_NAME]/Pictures".
File storageDir = FileUtils.getExternalFilesDir(mContext, Environment.DIRECTORY_PICTURES);
// Get app specific directory, e.g. "/storage/emulated/0/Android/media/[PACKAGE_NAME]/Pictures".
File storageDir = FileUtils.getExternalFilesDir(mContext, ExternalStorageDirType.INT_MEDIA, Environment.DIRECTORY_PICTURES);
if (storageDir == null) {
Log.e(TAG, "addSyncthingCameraFolder: storageDir == null");
return false;
Expand All @@ -1127,7 +1128,7 @@ private boolean addSyncthingCameraFolder() {
folder.versioning.fsType = "basic";

// Add folder to config.
LogV("addSyncthingCameraFolder: Adding folder to config ...");
LogV("addSyncthingCameraFolder: Adding folder to config [" + folder.path + "]");
addFolder(folder);
return true;
}
Expand Down
29 changes: 24 additions & 5 deletions app/src/main/java/com/nutomic/syncthingandroid/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ private FileUtils() {

public enum ExternalStorageDirType {
DATA,
MEDIA
EXT_MEDIA,
INT_MEDIA
}

@Nullable
Expand Down Expand Up @@ -183,18 +184,30 @@ public static File getExternalFilesDir(Context context, ExternalStorageDirType e
externalFilesDir.addAll(Arrays.asList(ContextCompat.getExternalFilesDirs(context, null)));
externalFilesDir.remove(context.getExternalFilesDir(null));
break;
case MEDIA:
case INT_MEDIA:
externalFilesDir.add(new File(Environment.getExternalStorageDirectory() + "/Android/media/" + context.getPackageName()));
break;
case EXT_MEDIA:
externalFilesDir.addAll(Arrays.asList(context.getExternalMediaDirs()));
if (externalFilesDir.size() > 0) {
if (!externalFilesDir.isEmpty()) {
externalFilesDir.remove(externalFilesDir.get(0));
}
break;
}
externalFilesDir.remove(null); // getExternalFilesDirs may return null for an ejected SDcard.
if (externalFilesDir.size() == 0) {
if (externalFilesDir.isEmpty()) {
Log.w(TAG, "Could not determine app's private files directory on external storage.");
return null;
}
if (type != null) {
switch(extDirType) {
case EXT_MEDIA:
case INT_MEDIA:
if (type.equals(Environment.DIRECTORY_PICTURES)) {
return new File(externalFilesDir.get(0), Environment.DIRECTORY_PICTURES);
}
}
}
return externalFilesDir.get(0);
}

Expand Down Expand Up @@ -233,12 +246,18 @@ public static android.net.Uri getExternalFilesDirUri(Context context, ExternalSt
"content://com.android.externalstorage.documents/document/" +
volumeId + "%3AAndroid%2Fdata%2F" +
context.getPackageName() + "%2Ffiles");
case MEDIA:
case EXT_MEDIA:
// Build the content Uri for our private ".../media/[PKG_NAME]" folder.
return android.net.Uri.parse(
"content://com.android.externalstorage.documents/document/" +
volumeId + "%3AAndroid%2Fmedia%2F" +
context.getPackageName());
case INT_MEDIA:
// Build the content Uri for our private ".../media/[PKG_NAME]" folder.
return android.net.Uri.parse(
"content://com.android.externalstorage.documents/document/" +
"primary" + "%3AAndroid%2Fmedia%2F" +
context.getPackageName());
}
} catch (Exception e) {
Log.w(TAG, "getExternalFilesDirUri exception", e);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" />
<external-media-path name="my_images" />
</paths>

0 comments on commit 679cbd0

Please sign in to comment.