Commit cb6db05d by Sebastián Katzer

Allow query in file path to avoid conflict with Windows

parent a75c0979
......@@ -36,7 +36,6 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
......@@ -106,7 +105,8 @@ public final class AssetUtil {
* @return URI pointing to the given path.
*/
private Uri getUriFromPath(String path) {
String absPath = path.replaceFirst("file://", "");
String absPath = path.replaceFirst("file://", "")
.replaceFirst("\\?.*$", "");
File file = new File(absPath);
if (!file.exists()) {
......@@ -125,7 +125,8 @@ public final class AssetUtil {
* @return URI pointing to the given path.
*/
private Uri getUriFromAsset(String path) {
String resPath = path.replaceFirst("file:/", "www");
String resPath = path.replaceFirst("file:/", "www")
.replaceFirst("\\?.*$", "");
String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
File file = getTmpFile(fileName);
......@@ -134,22 +135,16 @@ public final class AssetUtil {
try {
AssetManager assets = context.getAssets();
FileOutputStream outStream = new FileOutputStream(file);
InputStream inputStream = assets.open(resPath);
copyFile(inputStream, outStream);
outStream.flush();
outStream.close();
return getUriFromFile(file);
InputStream in = assets.open(resPath);
FileOutputStream out = new FileOutputStream(file);
copyFile(in, out);
} catch (Exception e) {
Log.e("Asset", "File not found: assets/" + resPath);
e.printStackTrace();
return Uri.EMPTY;
}
return Uri.EMPTY;
return getUriFromFile(file);
}
/**
......@@ -203,16 +198,11 @@ public final class AssetUtil {
connection.setConnectTimeout(5000);
connection.connect();
InputStream input = connection.getInputStream();
FileOutputStream outStream = new FileOutputStream(file);
copyFile(input, outStream);
outStream.flush();
outStream.close();
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(file);
copyFile(in, out);
return getUriFromFile(file);
} catch (MalformedURLException e) {
Log.e("Asset", "Incorrect URL");
e.printStackTrace();
......@@ -233,13 +223,19 @@ public final class AssetUtil {
* @param in The input stream.
* @param out The output stream.
*/
private void copyFile(InputStream in, OutputStream out) throws IOException {
private void copyFile(InputStream in, FileOutputStream out) {
byte[] buffer = new byte[1024];
int read;
try {
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment