将图像保存到内部存储器

时间:2020-02-14 19:33:40

标签: java android

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

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。当我从图库中选择图片并单击按钮进行保存时,什么也没有发生,程序开始滞后。我该怎么办?

1 个答案:

答案 0 :(得分:0)

这是你的问题。

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