|
4 | 4 | import android.content.Intent; |
5 | 5 | import android.content.SharedPreferences; |
6 | 6 | import android.content.pm.PackageManager; |
7 | | -import android.net.Uri; |
8 | 7 | import android.os.Build; |
9 | 8 | import android.os.Bundle; |
10 | 9 | import android.os.Environment; |
|
22 | 21 | import android.widget.Button; |
23 | 22 | import android.widget.ListView; |
24 | 23 | import android.widget.TextView; |
| 24 | +import android.widget.Toast; |
25 | 25 |
|
26 | 26 | import com.jpegkit.Jpeg; |
27 | 27 | import com.jpegkit.JpegFile; |
@@ -213,28 +213,16 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
213 | 213 | } |
214 | 214 |
|
215 | 215 | if (requestCode == JPEG_INTENT && resultCode == RESULT_OK) { |
216 | | - Set<String> jpegSet = mSharedPreferences.getStringSet("jpegs", new TreeSet<String>()); |
217 | | - |
218 | 216 | String name = data.getStringExtra("name"); |
219 | | - byte[] jpeg = data.getByteArrayExtra("jpeg"); |
220 | | - |
221 | | - if (name == null || jpeg == null) { |
222 | | - return; |
223 | | - } |
224 | 217 |
|
225 | | - for (String jpegPath : jpegSet) { |
226 | | - if (jpegPath.contains(name)) { |
227 | | - try { |
228 | | - File album = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "jpegkit"); |
229 | | - if (!album.mkdirs() && !album.exists()) { |
230 | | - return; |
| 218 | + if (name != null) { |
| 219 | + for (int i = 0; i < mAdapter.getCount(); i++) { |
| 220 | + JpegItem item = mAdapter.getItem(i); |
| 221 | + if (item.mJpeg.getFile().getAbsolutePath().contains(name)) { |
| 222 | + try { |
| 223 | + item.mJpeg.reload(); |
| 224 | + } catch (Exception e) { |
231 | 225 | } |
232 | | - |
233 | | - File imageFile = new File(album, name); |
234 | | - FileOutputStream outputStream = new FileOutputStream(imageFile); |
235 | | - outputStream.write(jpeg); |
236 | | - outputStream.close(); |
237 | | - } catch (Exception e) { |
238 | 226 | } |
239 | 227 | } |
240 | 228 | } |
@@ -299,26 +287,43 @@ public JpegAdapter() { |
299 | 287 | } |
300 | 288 |
|
301 | 289 | public void refresh() { |
302 | | - if (mData != null) { |
303 | | - for (JpegItem jpegItem : mData) { |
304 | | - if (jpegItem.mJpeg != null) { |
305 | | - jpegItem.mJpeg.release(); |
306 | | - } |
307 | | - } |
308 | | - } |
| 290 | + List<JpegItem> oldData = mData; |
309 | 291 |
|
310 | 292 | mData = new ArrayList<>(); |
311 | 293 |
|
312 | 294 | SharedPreferences sharedPreferences = getSharedPreferences("jpegkit", MODE_PRIVATE); |
313 | 295 | Set<String> jpegSet = sharedPreferences.getStringSet("jpegs", new TreeSet<String>()); |
314 | 296 | for (String jpegPath : jpegSet) { |
315 | | - try { |
316 | | - File file = new File(jpegPath); |
317 | | - mData.add(new JpegItem(file)); |
318 | | - } catch (Exception e) { |
| 297 | + boolean added = false; |
| 298 | + |
| 299 | + if (oldData != null) { |
| 300 | + for (JpegItem oldItem : oldData) { |
| 301 | + if (!added && oldItem.mFile.getAbsolutePath().contains(jpegPath)) { |
| 302 | + mData.add(oldItem); |
| 303 | + added = true; |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + if (!added) { |
| 309 | + try { |
| 310 | + File file = new File(jpegPath); |
| 311 | + mData.add(new JpegItem(file)); |
| 312 | + } catch (Exception e) { |
| 313 | + } |
319 | 314 | } |
320 | 315 | } |
321 | 316 |
|
| 317 | + if (oldData != null) { |
| 318 | + for (JpegItem oldItem : oldData) { |
| 319 | + if (!mData.contains(oldItem) && oldItem.mJpeg != null) { |
| 320 | + oldItem.mJpeg.release(); |
| 321 | + } |
| 322 | + } |
| 323 | + |
| 324 | + oldData.clear(); |
| 325 | + } |
| 326 | + |
322 | 327 | notifyDataSetChanged(); |
323 | 328 | } |
324 | 329 |
|
|
0 commit comments