mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-09 17:58:34 +00:00
parent
ed17701a0a
commit
a264d10685
@ -1,5 +1,6 @@
|
|||||||
package org.thoughtcrime.securesms.util;
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface.OnClickListener;
|
import android.content.DialogInterface.OnClickListener;
|
||||||
@ -28,6 +29,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.Objects;
|
||||||
|
|
||||||
public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTask.Attachment, Void, Pair<Integer, String>> {
|
public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTask.Attachment, Void, Pair<Integer, String>> {
|
||||||
private static final String TAG = SaveAttachmentTask.class.getSimpleName();
|
private static final String TAG = SaveAttachmentTask.class.getSimpleName();
|
||||||
@ -87,7 +89,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
|||||||
|
|
||||||
private @Nullable String saveAttachment(Context context, Attachment attachment) throws IOException
|
private @Nullable String saveAttachment(Context context, Attachment attachment) throws IOException
|
||||||
{
|
{
|
||||||
String contentType = MediaUtil.getCorrectedMimeType(attachment.contentType);
|
String contentType = Objects.requireNonNull(MediaUtil.getCorrectedMimeType(attachment.contentType));
|
||||||
String fileName = attachment.fileName;
|
String fileName = attachment.fileName;
|
||||||
|
|
||||||
if (fileName == null) fileName = generateOutputFileName(contentType, attachment.date);
|
if (fileName == null) fileName = generateOutputFileName(contentType, attachment.date);
|
||||||
@ -102,13 +104,13 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outputUri.equals(StorageUtil.getLegacyDownloadUri())) {
|
if (Objects.equals(outputUri.getScheme(), ContentResolver.SCHEME_FILE)) {
|
||||||
try (OutputStream outputStream = new FileOutputStream(mediaUri.getPath())) {
|
try (OutputStream outputStream = new FileOutputStream(mediaUri.getPath())) {
|
||||||
Util.copy(inputStream, outputStream);
|
Util.copy(inputStream, outputStream);
|
||||||
MediaScannerConnection.scanFile(context, new String[]{mediaUri.getPath()}, new String[]{contentType}, null);
|
MediaScannerConnection.scanFile(context, new String[]{mediaUri.getPath()}, new String[]{contentType}, null);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try (OutputStream outputStream = context.getContentResolver().openOutputStream(mediaUri)) {
|
try (OutputStream outputStream = context.getContentResolver().openOutputStream(mediaUri, "w")) {
|
||||||
Util.copy(inputStream, outputStream);
|
Util.copy(inputStream, outputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,7 +168,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
|||||||
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
|
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT <= 28 && outputUri.equals(StorageUtil.getLegacyDownloadUri())) {
|
if (Build.VERSION.SDK_INT <= 28 && Objects.equals(outputUri.getScheme(), ContentResolver.SCHEME_FILE)) {
|
||||||
File outputDirectory = new File(outputUri.getPath());
|
File outputDirectory = new File(outputUri.getPath());
|
||||||
File outputFile = new File(outputDirectory, base + "." + extension);
|
File outputFile = new File(outputDirectory, base + "." + extension);
|
||||||
|
|
||||||
|
@ -17,9 +17,7 @@ import org.thoughtcrime.securesms.BuildConfig;
|
|||||||
import org.thoughtcrime.securesms.R;
|
import org.thoughtcrime.securesms.R;
|
||||||
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
||||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||||
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
|
||||||
import org.thoughtcrime.securesms.permissions.Permissions;
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
||||||
import org.whispersystems.libsignal.util.guava.Optional;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -124,27 +122,39 @@ public class StorageUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static @NonNull Uri getVideoUri() {
|
public static @NonNull Uri getVideoUri() {
|
||||||
return MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
if (Build.VERSION.SDK_INT < 21) {
|
||||||
}
|
return getLegacyUri(Environment.DIRECTORY_MOVIES);
|
||||||
|
|
||||||
public static @NonNull Uri getAudioUri() {
|
|
||||||
return MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NonNull Uri getImageUri() {
|
|
||||||
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NonNull Uri getDownloadUri() {
|
|
||||||
if (Build.VERSION.SDK_INT > 28) {
|
|
||||||
return MediaStore.Downloads.EXTERNAL_CONTENT_URI;
|
|
||||||
} else {
|
} else {
|
||||||
return getLegacyDownloadUri();
|
return MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NonNull Uri getLegacyDownloadUri() {
|
public static @NonNull Uri getAudioUri() {
|
||||||
return Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
|
if (Build.VERSION.SDK_INT < 21) {
|
||||||
|
return getLegacyUri(Environment.DIRECTORY_MUSIC);
|
||||||
|
} else {
|
||||||
|
return MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NonNull Uri getImageUri() {
|
||||||
|
if (Build.VERSION.SDK_INT < 21) {
|
||||||
|
return getLegacyUri(Environment.DIRECTORY_PICTURES);
|
||||||
|
} else {
|
||||||
|
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NonNull Uri getDownloadUri() {
|
||||||
|
if (Build.VERSION.SDK_INT < 29) {
|
||||||
|
return getLegacyUri(Environment.DIRECTORY_DOWNLOADS);
|
||||||
|
} else {
|
||||||
|
return MediaStore.Downloads.EXTERNAL_CONTENT_URI;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NonNull Uri getLegacyUri(@NonNull String directory) {
|
||||||
|
return Uri.fromFile(Environment.getExternalStoragePublicDirectory(directory));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @Nullable String getCleanFileName(@Nullable String fileName) {
|
public static @Nullable String getCleanFileName(@Nullable String fileName) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user