Skip to content

Commit 62525b2

Browse files
committed
Merge pull request #47 from simon-liubin/fix/uri-to-file-1
从包含 file://path 的 Uri 中获取文件 new File(new URI(uri.toString())) -> new File(uri.getEncodedPath())。 path带空格等导致new URI() 解析报错
2 parents 2710485 + 7ad04f9 commit 62525b2

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/com/qiniu/io/IO.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,15 @@ public void onFailure(Exception ex) {
9494
*/
9595
public void putFile(Context mContext, String key, Uri uri, PutExtra extra, final JSONObjectRet ret) {
9696
if (!uri.toString().startsWith("file")) uri = convertFileUri(mContext, uri);
97-
try {
98-
File file = new File(new URI(uri.toString()));
99-
if (file.exists()) {
100-
putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
101-
return;
102-
}
103-
ret.onFailure(new Exception("file not exist: " + uri.toString()));
104-
} catch (URISyntaxException e) {
105-
e.printStackTrace();
106-
ret.onFailure(e);
97+
98+
File file = new File(uri.getEncodedPath());
99+
if (file.exists()) {
100+
putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
101+
return;
107102
}
103+
ret.onFailure(new Exception("file not exist: " + uri.toString()));
108104
}
105+
109106
public void putFile(String key, File file, PutExtra extra, JSONObjectRet callback) {
110107
putAndClose(key, InputStreamAt.fromFile(file), extra, callback);
111108
}

src/com/qiniu/resumableio/ResumableIO.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,13 @@ public int putFile(String key, File file, PutExtra extra, final JSONObjectRet re
149149

150150
public int putFile(Context mContext, String key, Uri uri, PutExtra extra, final JSONObjectRet ret) {
151151
if (!uri.toString().startsWith("file")) uri = convertFileUri(mContext, uri);
152-
try {
153-
File file = new File(new URI(uri.toString()));
154-
if (file.exists()) return putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
155-
ret.onFailure(new Exception("file not exist: " + uri.toString()));
156-
} catch (URISyntaxException e) {
157-
e.printStackTrace();
158-
ret.onFailure(e);
152+
153+
File file = new File(uri.getEncodedPath());
154+
if (file.exists()) {
155+
return putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
159156
}
157+
ret.onFailure(new Exception("file not exist: " + uri.toString()));
158+
160159
return -1;
161160
}
162161

0 commit comments

Comments
 (0)