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; ...@@ -36,7 +36,6 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
...@@ -106,8 +105,9 @@ public final class AssetUtil { ...@@ -106,8 +105,9 @@ public final class AssetUtil {
* @return URI pointing to the given path. * @return URI pointing to the given path.
*/ */
private Uri getUriFromPath(String path) { private Uri getUriFromPath(String path) {
String absPath = path.replaceFirst("file://", ""); String absPath = path.replaceFirst("file://", "")
File file = new File(absPath); .replaceFirst("\\?.*$", "");
File file = new File(absPath);
if (!file.exists()) { if (!file.exists()) {
Log.e("Asset", "File not found: " + file.getAbsolutePath()); Log.e("Asset", "File not found: " + file.getAbsolutePath());
...@@ -125,7 +125,8 @@ public final class AssetUtil { ...@@ -125,7 +125,8 @@ public final class AssetUtil {
* @return URI pointing to the given path. * @return URI pointing to the given path.
*/ */
private Uri getUriFromAsset(String 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); String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
File file = getTmpFile(fileName); File file = getTmpFile(fileName);
...@@ -133,23 +134,17 @@ public final class AssetUtil { ...@@ -133,23 +134,17 @@ public final class AssetUtil {
return Uri.EMPTY; return Uri.EMPTY;
try { try {
AssetManager assets = context.getAssets(); AssetManager assets = context.getAssets();
FileOutputStream outStream = new FileOutputStream(file); InputStream in = assets.open(resPath);
InputStream inputStream = assets.open(resPath); FileOutputStream out = new FileOutputStream(file);
copyFile(in, out);
copyFile(inputStream, outStream);
outStream.flush();
outStream.close();
return getUriFromFile(file);
} catch (Exception e) { } catch (Exception e) {
Log.e("Asset", "File not found: assets/" + resPath); Log.e("Asset", "File not found: assets/" + resPath);
e.printStackTrace(); e.printStackTrace();
return Uri.EMPTY;
} }
return Uri.EMPTY; return getUriFromFile(file);
} }
/** /**
...@@ -203,16 +198,11 @@ public final class AssetUtil { ...@@ -203,16 +198,11 @@ public final class AssetUtil {
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.connect(); connection.connect();
InputStream input = connection.getInputStream(); InputStream in = connection.getInputStream();
FileOutputStream outStream = new FileOutputStream(file); FileOutputStream out = new FileOutputStream(file);
copyFile(input, outStream);
outStream.flush();
outStream.close();
copyFile(in, out);
return getUriFromFile(file); return getUriFromFile(file);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
Log.e("Asset", "Incorrect URL"); Log.e("Asset", "Incorrect URL");
e.printStackTrace(); e.printStackTrace();
...@@ -233,12 +223,18 @@ public final class AssetUtil { ...@@ -233,12 +223,18 @@ public final class AssetUtil {
* @param in The input stream. * @param in The input stream.
* @param out The output 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]; byte[] buffer = new byte[1024];
int read; int read;
while ((read = in.read(buffer)) != -1) { try {
out.write(buffer, 0, read); 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