0

I am new in android studio and I am using Parse to store images. I am creating an image gallery and I am able to display downloaded images from parse to gridview. The problem is everytime I restart the app, my SDcard will download the exact same files from Parse. Is there a way to delete these unnecessary files? Or how can I put these Downloaded image files into a temporary storage where they will be deleted once the user is finished with the app? This is my code for downloading the images and store them in gridview:

        for (ParseObject imageOb : objects) {
                            final ParseFile file = (ParseFile) imageOb.get("image");

                            file.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] data, ParseException e) {
                                    if (e == null) {

                                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                                        Uri uri = getImageUri(getApplicationContext(), bitmap);

                                        File imageFiles = new File(getRealPathFromURI(uri));

                                        imageAdapter.add(imageFiles.toString());

                                    }

                                    imageAdapter.notifyDataSetChanged();
                                }

                            });

                        }
William Anputra
  • 91
  • 1
  • 1
  • 8
  • You should try to avoid that many levels of brackets, the code becomes hard to read. The first two if statements can be combined into a single if statement. – David Dec 13 '15 at 09:30
  • okay David, i will edit it – William Anputra Dec 13 '15 at 10:48
  • Did you search Stack Overflow for similar questions? This looks like it might be relevant: http://stackoverflow.com/questions/12317934/what-is-the-best-way-to-create-temporary-files-on-android Did you try manually deleting the files when the user is finished with your application? – David Dec 13 '15 at 12:43
  • @David it still failing. thanks for the suggestion though. i think the solution to my problem is just to get the image url and display it to my gridView then onDestroy, i delete all of the image URL ? – William Anputra Dec 13 '15 at 12:52
  • Yeah, but you don't specify in which part of the Android lifecycle you add the image. If you do it in `onStart`, `onDestroy` is fine; but if you download the images in `onResume` you should delete them in `onPause`. Check out this article on the Android lifecycle: http://developer.android.com/training/basics/activity-lifecycle/starting.html – David Dec 13 '15 at 12:59
  • @David aahh i see i see... i added the image inside onCreate. will it work? – William Anputra Dec 13 '15 at 13:02
  • I think it should work as you expect. – David Dec 13 '15 at 13:04
  • @David allright i'll try that – William Anputra Dec 13 '15 at 13:26

1 Answers1

0

There are some answers already on Stack overflow about temporary files, for example What is the best way to create temporary files

You could also manually delete the downloaded files when the application doesn't need them anymore. If you download them in onCreate you should delete them in onDestroy. Otherwise, check out this Article about the Android lifecycle

Community
  • 1
  • 1
David
  • 943
  • 1
  • 10
  • 26