Cannot Find Image Stored

自古美人都是妖i 提交于 2019-12-11 15:16:38

问题


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:


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



来源:https://stackoverflow.com/questions/51276641/cannot-find-image-stored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!