java - 将图像保存到内部存储
问题描述
我想在内部存储中保存一些图像。这是我的代码:
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。当我从我的图库中选择一张图片并单击按钮保存它时,什么也没有发生,程序开始滞后。我能做些什么?
解决方案
这条线是你的问题。
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();
}
推荐阅读
- python - SimPy - 模拟时钟
- android-studio - Android Studio 根据对象属性从数组列表中获取对象
- django - Django:如何将我的查询从我的 for 循环中取出
- ios - UIVisualEffectView 的子视图未在 XCUIApplication().debugDescription 中显示
- javascript - 使用 jquery 在编辑模式下显示多选值
- python - Pygame 按钮对点击没有反应
- date - 将日历上的日期与另一个字段匹配
- firebase - 如何通过颤动的火力使firestore持久化到缓存文档
- html - 有没有办法使用 Node JS 创建动态选项表单?
- laravel - Laravel8 有许多之一