DefaultAssetLoaderFactory: Simplify file extension retrival

Change the file extension retrieval back to how it was before 5488d33da8 to reduce the change of false negatives in `isImage()`

PiperOrigin-RevId: 605281186
This commit is contained in:
tofunmi 2024-02-08 04:57:48 -08:00 committed by Copybara-Service
parent 3a7a665d5d
commit 4d29d8f012
2 changed files with 7 additions and 31 deletions

View file

@ -19,7 +19,6 @@ import static androidx.media3.common.MimeTypes.normalizeMimeType;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
@ -30,7 +29,6 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/** Defines common file type constants and helper methods. */
@UnstableApi
@ -329,30 +327,4 @@ public final class FileTypes {
return FileTypes.UNKNOWN;
}
}
/**
* Returns the file extension of the given {@link Uri} or an empty string if there is no
* extension.
*
* <p>This method is a convenience method for obtaining the extension of a url and has undefined
* results for other Strings.
*/
public static String getFileExtensionFromUri(Uri uri) {
String path = uri.getPath();
if (TextUtils.isEmpty(path)) {
return "";
}
int filenamePos = path.lastIndexOf('/');
String filename = 0 <= filenamePos ? path.substring(filenamePos + 1) : path;
// If the filename contains special characters, we don't consider it valid for our matching
// purposes.
if (!filename.isEmpty() && Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)\\%]+", filename)) {
int dotPos = filename.lastIndexOf('.');
if (0 <= dotPos) {
return filename.substring(dotPos + 1);
}
}
return "";
}
}

View file

@ -16,6 +16,7 @@
package androidx.media3.transformer;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
import android.content.ContentResolver;
@ -24,7 +25,6 @@ import android.graphics.BitmapFactory;
import android.graphics.ColorSpace;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.media3.common.FileTypes;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.BitmapLoader;
@ -175,8 +175,12 @@ public final class DefaultAssetLoaderFactory implements AssetLoader.Factory {
ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(localConfiguration.uri);
} else {
String fileExtension = FileTypes.getFileExtensionFromUri(localConfiguration.uri);
mimeType = getCommonImageMimeTypeFromExtension(Ascii.toLowerCase(fileExtension));
String uriPath = checkNotNull(localConfiguration.uri.getPath());
int fileExtensionStart = uriPath.lastIndexOf(".");
if (fileExtensionStart != -1) {
String extension = Ascii.toLowerCase(uriPath.substring(fileExtensionStart + 1));
mimeType = getCommonImageMimeTypeFromExtension(Ascii.toLowerCase(extension));
}
}
}
if (mimeType == null) {