|
24 | 24 | import java.io.IOException;
|
25 | 25 | import java.io.InputStream;
|
26 | 26 | import java.net.HttpURLConnection;
|
27 |
| -import java.net.URI; |
28 |
| -import java.net.URISyntaxException; |
29 | 27 | import java.net.URL;
|
30 | 28 |
|
31 | 29 | import android.util.Log;
|
32 | 30 |
|
33 | 31 | /**
|
34 |
| - * @title 根据 图片url地址下载图片 可以是本地和网络 |
35 |
| - * @author 杨福海(michael) www.yangfuhai.com |
| 32 | + * @title 根据 图片url地址下载图片 可以是本地和网络 |
| 33 | + * @author 杨福海(michael) www.yangfuhai.com |
36 | 34 | */
|
37 |
| -public class SimpleDownloader implements Downloader{ |
38 |
| - |
| 35 | +public class SimpleDownloader implements Downloader { |
| 36 | + |
39 | 37 | 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 | + |
43 | 40 | 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 | + } |
61 | 52 |
|
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + private byte[] getFromFile(File file) { |
62 | 57 |
|
63 |
| - private byte[] getFromFile(String urlString) { |
| 58 | + FileReader fr = null; |
64 | 59 | try {
|
| 60 | + fr = new FileReader(file); |
65 | 61 | 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(); |
72 | 67 | } 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 | + } |
74 | 77 | }
|
75 |
| - |
| 78 | + |
76 | 79 | return null;
|
77 | 80 | }
|
78 |
| - |
79 |
| - |
80 | 81 |
|
81 | 82 | private byte[] getFromHttp(String urlString) {
|
82 | 83 | HttpURLConnection urlConnection = null;
|
83 | 84 | BufferedOutputStream out = null;
|
84 | 85 | FlushedInputStream in = null;
|
85 | 86 |
|
86 | 87 | 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(); |
96 | 98 | } catch (final IOException e) {
|
97 |
| - Log.e(TAG, "Error in downloadBitmap - "+urlString +" : " + e); |
| 99 | + Log.e(TAG, "Error in downloadBitmap - " + urlString + " : " + e); |
98 | 100 | } 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 | + } |
110 | 113 | }
|
111 | 114 | return null;
|
112 | 115 | }
|
113 | 116 |
|
114 |
| - |
115 |
| - public class FlushedInputStream extends FilterInputStream { |
| 117 | + public class FlushedInputStream extends FilterInputStream { |
116 | 118 |
|
117 |
| - public FlushedInputStream(InputStream inputStream) { |
118 |
| - super(inputStream); |
119 |
| - } |
| 119 | + public FlushedInputStream(InputStream inputStream) { |
| 120 | + super(inputStream); |
| 121 | + } |
120 | 122 |
|
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 | + } |
139 | 141 | }
|
0 commit comments