Skip to content

Commit 9c0eae3

Browse files
committed
demo app updates
1 parent 5ac401f commit 9c0eae3

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

app/src/main/java/com/jpegkit/app/JpegActivity.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,14 @@ public boolean onCreateOptionsMenu(Menu menu) {
110110
@Override
111111
public boolean onOptionsItemSelected(MenuItem item) {
112112
if (item.getItemId() == R.id.action_save) {
113+
try {
114+
mJpeg.save();
115+
} catch (Exception e) {
116+
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
117+
}
118+
113119
Intent data = new Intent();
114120
data.putExtra("name", mName);
115-
data.putExtra("jpeg", mJpegBytes);
116121
setResult(RESULT_OK, data);
117122
finish();
118123
return true;
@@ -139,7 +144,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
139144
}
140145

141146
if (item.getItemId() == R.id.action_metadata) {
142-
143147
return true;
144148
}
145149

app/src/main/java/com/jpegkit/app/MainActivity.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.content.Intent;
55
import android.content.SharedPreferences;
66
import android.content.pm.PackageManager;
7-
import android.net.Uri;
87
import android.os.Build;
98
import android.os.Bundle;
109
import android.os.Environment;
@@ -22,6 +21,7 @@
2221
import android.widget.Button;
2322
import android.widget.ListView;
2423
import android.widget.TextView;
24+
import android.widget.Toast;
2525

2626
import com.jpegkit.Jpeg;
2727
import com.jpegkit.JpegFile;
@@ -213,28 +213,16 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
213213
}
214214

215215
if (requestCode == JPEG_INTENT && resultCode == RESULT_OK) {
216-
Set<String> jpegSet = mSharedPreferences.getStringSet("jpegs", new TreeSet<String>());
217-
218216
String name = data.getStringExtra("name");
219-
byte[] jpeg = data.getByteArrayExtra("jpeg");
220-
221-
if (name == null || jpeg == null) {
222-
return;
223-
}
224217

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) {
231225
}
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) {
238226
}
239227
}
240228
}
@@ -299,26 +287,43 @@ public JpegAdapter() {
299287
}
300288

301289
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;
309291

310292
mData = new ArrayList<>();
311293

312294
SharedPreferences sharedPreferences = getSharedPreferences("jpegkit", MODE_PRIVATE);
313295
Set<String> jpegSet = sharedPreferences.getStringSet("jpegs", new TreeSet<String>());
314296
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+
}
319314
}
320315
}
321316

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+
322327
notifyDataSetChanged();
323328
}
324329

0 commit comments

Comments
 (0)