mirror of
https://github.com/apache/cordova-android.git
synced 2025-04-07 15:23:09 +08:00
Make URL parsing more robust in FileHelper.
Fixes some cases when query parameters mess things up.
This commit is contained in:
parent
cb99ed0a01
commit
791574c26e
@ -28,6 +28,8 @@ import org.apache.cordova.api.LOG;
|
|||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
public class FileHelper {
|
public class FileHelper {
|
||||||
private static final String LOG_TAG = "FileUtils";
|
private static final String LOG_TAG = "FileUtils";
|
||||||
@ -92,7 +94,8 @@ public class FileHelper {
|
|||||||
Uri uri = Uri.parse(uriString);
|
Uri uri = Uri.parse(uriString);
|
||||||
return cordova.getActivity().getContentResolver().openInputStream(uri);
|
return cordova.getActivity().getContentResolver().openInputStream(uri);
|
||||||
} else if (uriString.startsWith("file:///android_asset/")) {
|
} else if (uriString.startsWith("file:///android_asset/")) {
|
||||||
String relativePath = uriString.substring(22);
|
Uri uri = Uri.parse(uriString);
|
||||||
|
String relativePath = uri.getPath().substring(15);
|
||||||
return cordova.getActivity().getAssets().open(relativePath);
|
return cordova.getActivity().getAssets().open(relativePath);
|
||||||
} else {
|
} else {
|
||||||
return new FileInputStream(getRealPath(uriString, cordova));
|
return new FileInputStream(getRealPath(uriString, cordova));
|
||||||
@ -122,14 +125,18 @@ public class FileHelper {
|
|||||||
public static String getMimeType(String uriString, CordovaInterface cordova) {
|
public static String getMimeType(String uriString, CordovaInterface cordova) {
|
||||||
String mimeType = null;
|
String mimeType = null;
|
||||||
|
|
||||||
if (uriString.startsWith("content://")) {
|
|
||||||
Uri uri = Uri.parse(uriString);
|
Uri uri = Uri.parse(uriString);
|
||||||
|
if (uriString.startsWith("content://")) {
|
||||||
mimeType = cordova.getActivity().getContentResolver().getType(uri);
|
mimeType = cordova.getActivity().getContentResolver().getType(uri);
|
||||||
} else {
|
} else {
|
||||||
// MimeTypeMap.getFileExtensionFromUrl has a bug that occurs when the filename has a space, so we encode it.
|
// MimeTypeMap.getFileExtensionFromUrl() fails when there are query parameters.
|
||||||
// We also convert the URI string to lower case to ensure compatibility with MimeTypeMap (see CB-2185).
|
String extension = uri.getPath();
|
||||||
String encodedUriString = uriString.replace(" ", "%20").toLowerCase();
|
int lastDot = extension.lastIndexOf('.');
|
||||||
String extension = MimeTypeMap.getFileExtensionFromUrl(encodedUriString);
|
if (lastDot != -1) {
|
||||||
|
extension = extension.substring(lastDot + 1);
|
||||||
|
}
|
||||||
|
// Convert the URI string to lower case to ensure compatibility with MimeTypeMap (see CB-2185).
|
||||||
|
extension = extension.toLowerCase();
|
||||||
if (extension.equals("3ga")) {
|
if (extension.equals("3ga")) {
|
||||||
mimeType = "audio/3gpp";
|
mimeType = "audio/3gpp";
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user