找不到已存储的图像

时间:2018-07-11 03:02:30

标签: java android image android-bitmap android-storage

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

路径是这样的:

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

我在首页上这样称呼它:

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方法:

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

此代码的重点是使图像脱机可用,因此可以将其从Internet下载一次,然后指向并像这样加载:

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

                    homeSuggestPic.setImageBitmap(bitmap);

我试图找到它存储在其中的目录,以确保它实际上存储在设备上。不幸的是,使用文件管理器时,我的应用程序名称甚至都不会显示在数据下或任何地方。想法,我存储不正确吗?

1 个答案:

答案 0 :(得分:0)

该代码实际上有效,我犯了一个错误,即仅存储图像的索引0。我会将代码留给任何想使用它的人,只需更改此行String pictureToCheck = menuItems.get(0).getPicture();    为实际的循环图像获取0