1

When I download an image and save it to the Android device the image does not appear in the gallery, later after the phone is restarted the images are in the gallery.

Here is the code where I download the images and save them to the device:

private void downloadImage() {
        if (future != null) {
            //set the callback and start downloading
            future.withResponse().setCallback(new FutureCallback<Response<InputStream>>() {
                @Override
                public void onCompleted(Exception e, Response<InputStream> result) {
                    boolean success = false;
                    if (e == null && result != null && result.getResult() != null) {
                        try {
                            //prepare the file name
                            String url = mSelectedImage.getUrl();
                            String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
                            //create a temporary directory within the cache folder
                            File dir = Utils.getAlbumStorageDir("wall-tumbler");
                            //create the file
                            File file = new File(dir, fileName);
                            if (!file.exists()) {
                                file.createNewFile();
                            }

                            //copy the image onto this file
                            Utils.copyInputStreamToFile(result.getResult(), file);

                            //animate the first elements
                            animateCompleteFirst(true);

                            success = true;
                        } catch (Exception ex) {
                            Log.e("walltumbler", ex.toString());
                        }

                        //animate after complete
                        animateComplete(success);
                    } else {
                        animateReset(true);
                    }
                }
            });
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void downloadAndSetOrShareImage(final boolean set) {
        if (future != null) {
            //set the callback and start downloading
            future.withResponse().setCallback(new FutureCallback<Response<InputStream>>() {
                @Override
                public void onCompleted(Exception e, Response<InputStream> result) {
                    boolean success = false;
                    if (e == null && result != null && result.getResult() != null) {
                        try {
                            //create a temporary directory within the cache folder
                            File dir = new File(DetailActivity.this.getCacheDir() + "/images");
                            if (!dir.exists()) {
                                dir.mkdirs();
                            }

                            //create the file
                            File file = new File(dir, "walltumbler.jpg");
                            if (!file.exists()) {
                                file.createNewFile();
                            }

                            //copy the image onto this file
                            Utils.copyInputStreamToFile(result.getResult(), file);

                            //get the contentUri for this file and start the intent
                            Uri contentUri = FileProvider.getUriForFile(DetailActivity.this, "com.mikepenz.fileprovider", file);

                            if (set) {
                                //get crop intent
                                Intent intent = WallpaperManager.getInstance(DetailActivity.this).getCropAndSetWallpaperIntent(contentUri);
                                //start activity for result so we can animate if we finish
                                DetailActivity.this.startActivityForResult(intent, ACTIVITY_CROP);
                            } else {
                                //share :D
                                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                                shareIntent.setData(contentUri);
                                shareIntent.setType("image/jpg");
                                shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
                                //start activity for result so we can animate if we finish
                                DetailActivity.this.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), ACTIVITY_SHARE);
                            }

                            success = true;
                        } catch (Exception ex) {
                            Log.e("walltumbler", ex.toString());
                        }

                        //animate after complete
                        animateComplete(success);
                    } else {
                        animateReset(true);
                    }
                }
            });
        }
    }

Utils

/**
     * http://developer.android.com/training/basics/data-storage/files.html
     *
     * @param albumName
     * @return
     */

    public static File getAlbumStorageDir(String albumName) {
        // Get the directory for the user's public pictures directory.
        boolean success = false;
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
        if (!file.exists()) {
            success = file.mkdir();
        }
        if (!success)
            Log.i("wall-tumbler", "Directory not created");
        else
            Log.i("wall-tumbler", "Directory created");
        return file;
    }
    /**
     * http://developer.android.com/training/basics/data-storage/files.html
     *
     * @return
     */
    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }
}

ScreenShot https://i.stack.imgur.com/6dkp0.jpg

BrentM
  • 5,671
  • 3
  • 31
  • 38
Berat Şen
  • 95
  • 1
  • 7

1 Answers1

0

You should use the MediaStore content provider to add an image to the gallery.

ContentValues values = new ContentValues();
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, imagePath);
context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
BrentM
  • 5,671
  • 3
  • 31
  • 38
  • Call this code after you have saved the image to the external storage of the device. The imagePath will be the path to the file you downloaded. – BrentM Jun 09 '15 at 21:52
  • Maybe this thread will help: http://stackoverflow.com/questions/20859584/how-save-image-in-android-gallery – BrentM Jun 09 '15 at 22:07
  • 1
    Here is a slightly different solution, that might also work: http://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android – BrentM Jun 09 '15 at 22:12