无法找到存储的图像 [英] Cannot Find Image Stored

查看:170
本文介绍了无法找到存储的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用此代码存储图片(可能是错误的)。

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

路径是这样的:

public static final String IMAGE_DIR = "test";

保存图片的代码如下:

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);

这是getBitmap方法:

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?

推荐答案

代码实际工作我犯了一个错误,一直只存储图像的索引0 。我将为任何想要使用它的人留下代码只需更改此行 String pictureToCheck = menuItems.get(0).getPicture();
for get 0到实际的循环图像

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

这篇关于无法找到存储的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆