Skip to content

Commit 938dc25

Browse files
committed
修复 图片加载无法读取本地图片的bug
1 parent 09c91f0 commit 938dc25

File tree

1 file changed

+83
-81
lines changed

1 file changed

+83
-81
lines changed

src/net/tsz/afinal/bitmap/download/SimpleDownloader.java

Lines changed: 83 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -24,116 +24,118 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.net.HttpURLConnection;
27-
import java.net.URI;
28-
import java.net.URISyntaxException;
2927
import java.net.URL;
3028

3129
import android.util.Log;
3230

3331
/**
34-
* @title 根据 图片url地址下载图片 可以是本地和网络
35-
* @author 杨福海(michael) www.yangfuhai.com
32+
* @title 根据 图片url地址下载图片 可以是本地和网络
33+
* @author 杨福海(michael) www.yangfuhai.com
3634
*/
37-
public class SimpleDownloader implements Downloader{
38-
35+
public class SimpleDownloader implements Downloader {
36+
3937
private static final String TAG = "SimpleHttpDownloader";
40-
private static final int IO_BUFFER_SIZE = 8 * 1024; //8k
41-
42-
38+
private static final int IO_BUFFER_SIZE = 8 * 1024; // 8k
39+
4340
public byte[] download(String urlString) {
44-
if(urlString == null)
45-
return null;
46-
47-
if(urlString.trim().toLowerCase().startsWith("http")){
48-
return getFromHttp(urlString);
49-
}else if(urlString.trim().toLowerCase().startsWith("file")){
50-
try {
51-
return getFromFile(new URI(urlString).getPath());
52-
} catch (URISyntaxException e) {
53-
e.printStackTrace();
54-
}
55-
}else if(urlString.trim().toLowerCase().startsWith("/")){
56-
return getFromFile(urlString);
57-
}
58-
59-
return null;
60-
}
41+
if (urlString == null)
42+
return null;
43+
44+
if (urlString.trim().toLowerCase().startsWith("http")) {
45+
return getFromHttp(urlString);
46+
}else {
47+
File f = new File(urlString);
48+
if (f.exists() && f.canRead()) {
49+
return getFromFile(f);
50+
}
51+
}
6152

53+
return null;
54+
}
55+
56+
private byte[] getFromFile(File file) {
6257

63-
private byte[] getFromFile(String urlString) {
58+
FileReader fr = null;
6459
try {
60+
fr = new FileReader(file);
6561
ByteArrayOutputStream baos = new ByteArrayOutputStream();
66-
FileReader f = new FileReader(new File(urlString));
67-
int b;
68-
while ((b = f.read()) != -1) {
69-
baos.write(b);
70-
}
71-
return baos.toByteArray();
62+
int b = 0;
63+
while ((b = fr.read()) != -1) {
64+
baos.write(b);
65+
}
66+
return baos.toByteArray();
7267
} catch (Exception e) {
73-
e.printStackTrace();
68+
Log.e(TAG, "Error in read from file - " + file + " : " + e);
69+
} finally {
70+
if (fr != null) {
71+
try {
72+
fr.close();
73+
} catch (IOException e) {
74+
// do nothing
75+
}
76+
}
7477
}
75-
78+
7679
return null;
7780
}
78-
79-
8081

8182
private byte[] getFromHttp(String urlString) {
8283
HttpURLConnection urlConnection = null;
8384
BufferedOutputStream out = null;
8485
FlushedInputStream in = null;
8586

8687
try {
87-
final URL url = new URL(urlString);
88-
urlConnection = (HttpURLConnection) url.openConnection();
89-
in = new FlushedInputStream(new BufferedInputStream(urlConnection.getInputStream(), IO_BUFFER_SIZE));
90-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
91-
int b;
92-
while ((b = in.read()) != -1) {
93-
baos.write(b);
94-
}
95-
return baos.toByteArray();
88+
final URL url = new URL(urlString);
89+
urlConnection = (HttpURLConnection) url.openConnection();
90+
in = new FlushedInputStream(new BufferedInputStream(
91+
urlConnection.getInputStream(), IO_BUFFER_SIZE));
92+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
93+
int b;
94+
while ((b = in.read()) != -1) {
95+
baos.write(b);
96+
}
97+
return baos.toByteArray();
9698
} catch (final IOException e) {
97-
Log.e(TAG, "Error in downloadBitmap - "+urlString +" : " + e);
99+
Log.e(TAG, "Error in downloadBitmap - " + urlString + " : " + e);
98100
} finally {
99-
if (urlConnection != null) {
100-
urlConnection.disconnect();
101-
}
102-
try {
103-
if (out != null) {
104-
out.close();
105-
}
106-
if (in != null) {
107-
in.close();
108-
}
109-
} catch (final IOException e) {}
101+
if (urlConnection != null) {
102+
urlConnection.disconnect();
103+
}
104+
try {
105+
if (out != null) {
106+
out.close();
107+
}
108+
if (in != null) {
109+
in.close();
110+
}
111+
} catch (final IOException e) {
112+
}
110113
}
111114
return null;
112115
}
113116

114-
115-
public class FlushedInputStream extends FilterInputStream {
117+
public class FlushedInputStream extends FilterInputStream {
116118

117-
public FlushedInputStream(InputStream inputStream) {
118-
super(inputStream);
119-
}
119+
public FlushedInputStream(InputStream inputStream) {
120+
super(inputStream);
121+
}
120122

121-
@Override
122-
public long skip(long n) throws IOException {
123-
long totalBytesSkipped = 0L;
124-
while (totalBytesSkipped < n) {
125-
long bytesSkipped = in.skip(n - totalBytesSkipped);
126-
if (bytesSkipped == 0L) {
127-
int by_te = read();
128-
if (by_te < 0) {
129-
break; // we reached EOF
130-
} else {
131-
bytesSkipped = 1; // we read one byte
132-
}
133-
}
134-
totalBytesSkipped += bytesSkipped;
135-
}
136-
return totalBytesSkipped;
137-
}
138-
}
123+
@Override
124+
public long skip(long n) throws IOException {
125+
long totalBytesSkipped = 0L;
126+
while (totalBytesSkipped < n) {
127+
long bytesSkipped = in.skip(n - totalBytesSkipped);
128+
if (bytesSkipped == 0L) {
129+
int by_te = read();
130+
if (by_te < 0) {
131+
break; // we reached EOF
132+
} else {
133+
bytesSkipped = 1; // we read one byte
134+
}
135+
}
136+
totalBytesSkipped += bytesSkipped;
137+
}
138+
return totalBytesSkipped;
139+
}
140+
}
139141
}

0 commit comments

Comments
 (0)