Cannot Find Image Stored

I am currently using this code to store images (it might be wrong).

The path is this:

public static final String IMAGE_DIR = "test";

The code for saving the image is as follow:

public class ImageSaver {

    private String directoryName = Constants.IMAGE_DIR;
    private String fileName = "";
    private Context context;
    private boolean external;

    public ImageSaver(Context context) {
        this.context = context;
    }

    public ImageSaver setFileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    public ImageSaver setExternal(boolean external) {
        this.external = external;
        return this;
    }

    public ImageSaver setDirectoryName(String directoryName) {
        this.directoryName = directoryName;
        return this;
    }

    public int save(Bitmap bitmapImage, int jpgOrPng) {
        FileOutputStream fileOutputStream = null;
        try {

            fileOutputStream = new FileOutputStream(createFile());
            if(jpgOrPng == 0) {
                bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

            } else {

                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            }
            Log.d("THE PICTURE ", " The picture finished saving");

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                return 1;
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
        }
    }

    @NonNull
    private File createFile() {
        File directory;
        if(external){
            directory = getAlbumStorageDir(directoryName);
        }
        else {
            directory = context.getDir(directoryName, Context.MODE_PRIVATE);
        }
        if(!directory.exists() && !directory.mkdirs()){
            Log.e("ImageSaver","Error creating directory " + directory);
        }

        return new File(directory, fileName);
    }

    private File getAlbumStorageDir(String albumName) {
        return new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), albumName);
    }

    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }

    public static boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
    }

    public Bitmap load() {
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(createFile());
            return BitmapFactory.decodeStream(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean deleteFile(){ File file = createFile(); return file.delete(); }
}

I call it like so on my home page:

Bitmap foodImage = getImageBitmapFromURL(this, Constants.FOOD_IMAGE + menuItemReponseList.get(storeItemCounter).getPicture());
        String pictureToCheck = menuItems.get(0).getPicture();
        String[] words = pictureToCheck.split(Pattern.quote("."));
        String newPic = words[0];
    int jpgOrPng = 0;
        if(newPic.equals("png")) {
            jpgOrPng = 1;
            Log.d("THE PICTURE IS ", "Poc  png: " + newPic);
        }
        int done = new ImageSaver(this).
                    setFileName(menuItems.get(0).getPicture()).
                    setDirectoryName(Constants.IMAGE_DIR).
                    save(foodImage, jpgOrPng);

Here is the getBitmap method:

public static Bitmap getImageBitmapFromURL(final Context context, final String imageUrl){

        Bitmap imageBitmap = null;
        try {
            imageBitmap = new AsyncTask<Void, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(Void... params) {
                    try {
                        int targetHeight = 200;
                        int targetWidth = 300;

                        return Picasso.get().load(String.valueOf(imageUrl))
                                .resize(targetWidth, targetHeight)
                                .placeholder(R.drawable.burger)
                                .error(R.drawable.burger)
                                .get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            }.execute().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return imageBitmap;
    }

The point of this code is to make the images available offline, so they can be downloaded once off the internet and then pointed to and loaded like so:

  Bitmap bitmap = new ImageSaver(this).
                                setFileName(newPic).
                                setDirectoryName(Constants.IMAGE_DIR).
                                load();

                    homeSuggestPic.setImageBitmap(bitmap);

I was trying to find the directory it was stored in to make sure that it is actually being stored on the device. Unfortunately, when using file manager my app name does not even show up under data or anywhere for that matter. Thoughts, am I storing it incorrectly?

1 answer

  • answered 2018-07-12 02:21 Lion789

    The code actually works I made a mistake an kept storing only index 0 of the image. I will leave the code up for anyone that wants to use it just change this line String pictureToCheck = menuItems.get(0).getPicture(); for get 0 to the actual looped image