Try a little harder to find a place to store the file before accepting a directory path that may not exist.

Fixes #11505
This commit is contained in:
Fumiaki Yoshimatsu 2021-08-05 09:56:27 -04:00 committed by Alex Hart
parent 89b1243885
commit 555e65d3ee

View file

@ -32,6 +32,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@ -152,17 +153,53 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
}
}
public String getExternalPathToFileForType(String contentType) {
File storage;
private @Nullable File ensureExternalPath(@Nullable File path) {
if (path != null && path.exists()) {
return path;
}
if (path == null) {
File documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
if (documents.exists() || documents.mkdirs()) {
return documents;
} else {
return null;
}
}
if (path.mkdirs()) {
return path;
} else {
return null;
}
}
/**
* Returns a path to a shared media (or documents) directory for the type of the file.
*
* Note that this method attempts to create a directory if the path returned from
* Environment object does not exist yet. The attempt may fail in which case it attempts
* to return the default "Document" path. It finally returns null if it also fails.
* Otherwise it returns the absolute path to the directory.
*
* @param contentType a MIME type of a file
* @return an absolute path to a directory or null
*/
private @Nullable String getExternalPathForType(String contentType) {
File storage = null;
if (contentType.startsWith("video/")) {
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
} else if (contentType.startsWith("audio/")) {
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
} else if (contentType.startsWith("image/")) {
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
} else {
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
}
storage = ensureExternalPath(storage);
if (storage == null) {
return null;
}
return storage.getAbsolutePath();
}
@ -217,13 +254,18 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
return Uri.fromFile(outputFile);
} else {
String dir = getExternalPathForType(contentType);
if (dir == null) {
throw new IOException(String.format(Locale.US, "Path for type: %s was not available", contentType));
}
String outputFileName = fileName;
String dataPath = String.format("%s/%s", getExternalPathToFileForType(contentType), outputFileName);
String dataPath = String.format("%s/%s", dir, outputFileName);
int i = 0;
while (pathTaken(outputUri, dataPath)) {
Log.d(TAG, "The content exists. Rename and check again.");
outputFileName = base + "-" + (++i) + "." + extension;
dataPath = String.format("%s/%s", getExternalPathToFileForType(contentType), outputFileName);
dataPath = String.format("%s/%s", dir, outputFileName);
}
contentValues.put(MediaStore.MediaColumns.DATA, dataPath);
}