首页 > 解决方案 > 将图像保存到内部存储

问题描述

我想在内部存储中保存一些图像。这是我的代码:

Bitmap bitmap = ((BitmapDrawable)iv_add.getDrawable()).getBitmap();
            File file = getApplicationContext().getDir("Images",MODE_PRIVATE);
            file = new File(file, "UniqueFileName"+".jpg");
            try{
                OutputStream stream = null;
                stream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
                stream.flush();
                stream.close();
            }catch (IOException e)
            {
                e.printStackTrace();
            }

据我了解,图片需要进入 internal_storage/android/data/project_name/file。当我从我的图库中选择一张图片并单击按钮保存它时,什么也没有发生,程序开始滞后。我能做些什么?

标签: javaandroid

解决方案


这条线是你的问题。

ContextWrapper wrapper = new ContextWrapper(getApplicationContext());

您不应该自己创建上下文包装器。

        File file = getApplicationContext().getDir("Images",MODE_PRIVATE);
        file = new File(file, "UniqueFileName"+".jpg");
        try{
            OutputStream stream = null;
            stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
            stream.flush();
            stream.close();
        }catch (IOException e)
        {
            e.printStackTrace();
        }

推荐阅读