kopia lustrzana https://github.com/ryukoposting/Signal-Android
Try a little harder to find a place to store the file before accepting a directory path that may not exist.
Fixes #11505fork-5.53.8
rodzic
89b1243885
commit
555e65d3ee
|
@ -32,6 +32,7 @@ import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -152,17 +153,53 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExternalPathToFileForType(String contentType) {
|
private @Nullable File ensureExternalPath(@Nullable File path) {
|
||||||
File storage;
|
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/")) {
|
if (contentType.startsWith("video/")) {
|
||||||
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
|
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
|
||||||
} else if (contentType.startsWith("audio/")) {
|
} else if (contentType.startsWith("audio/")) {
|
||||||
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
|
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
|
||||||
} else if (contentType.startsWith("image/")) {
|
} else if (contentType.startsWith("image/")) {
|
||||||
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||||
} else {
|
|
||||||
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
storage = ensureExternalPath(storage);
|
||||||
|
if (storage == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return storage.getAbsolutePath();
|
return storage.getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,13 +254,18 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||||
|
|
||||||
return Uri.fromFile(outputFile);
|
return Uri.fromFile(outputFile);
|
||||||
} else {
|
} 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 outputFileName = fileName;
|
||||||
String dataPath = String.format("%s/%s", getExternalPathToFileForType(contentType), outputFileName);
|
String dataPath = String.format("%s/%s", dir, outputFileName);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (pathTaken(outputUri, dataPath)) {
|
while (pathTaken(outputUri, dataPath)) {
|
||||||
Log.d(TAG, "The content exists. Rename and check again.");
|
Log.d(TAG, "The content exists. Rename and check again.");
|
||||||
outputFileName = base + "-" + (++i) + "." + extension;
|
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);
|
contentValues.put(MediaStore.MediaColumns.DATA, dataPath);
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue