历览千载书,时时见遗烈。这篇文章主要讲述android 存储图片到data目录和读取data目录下的图片相关的知识,希望能为你提供帮助。
public void storePic(String tabid, String key, Bitmap bitmap) {
LogUtils.LOGD(TAG, "storePic begin tabid = " + tabid + "key = " + key);
FileOutputStream fos = null;
try {
fos = getActivity().openFileOutput(tabid + "_" + key, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
LogUtils.LOGE(TAG, "storePic FileNotFoundException e = " +e);
} finally {
if(fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
LogUtils.LOGE(TAG, "storePic IOException e = " +e);
}
}
}
}
public Bitmap getStorePic(String tabid, String key) {
LogUtils.LOGD(TAG, "getStorePic begin tabid = " + tabid + "key = " + key);
FileInputStream fin = null;
Bitmap bitmap = null;
try {
fin = getActivity().openFileInput(tabid + "_" + key);
bitmap = BitmapFactory.decodeStream(fin);
} catch (FileNotFoundException e) {
LogUtils.LOGE(TAG, "getStorePic FileNotFoundException e = " + e);
}
return bitmap;
}
总而流程:
存储图片代码:
[java] view plain copy
- String str1 = "icon.png";
- FileOutputStream localFileOutputStream1 = openFileOutput(str1, 0);
- Bitmap.CompressFormat localCompressFormat = Bitmap.CompressFormat.PNG;
- bitmap.compress(localCompressFormat, 100, localFileOutputStream1);
- localFileOutputStream1.close();
【android 存储图片到data目录和读取data目录下的图片】读取图片代码:
[java] view plain copy
- String localIconNormal = "icon.png";
- FileInputStream localStream = openFileInput(localIconNormal);
- Bitmap bitmap = BitmapFactory.decodeStream(localStream));
推荐阅读
- Android启动标记
- URL转Drawable之 Android中获取网络图片的三种方法
- Android 中使用MediaRecorder实现视频录制功能
- 在Android中Intent的概念及应用——Intent过滤器相关选项
- 前16名最佳Grammarly替代品和免费语法检查器推荐合集
- 如何在Google Chrome浏览器中打开拼写检查(操作方法教程)
- 14大假电子邮件生成器推荐合集(用于获取临时电子邮件地址)
- 如何增加Windows 10耳机和扬声器的低音(你知道哪些方法?)
- 如何使用任务计划程序自动关闭Windows 10(操作方法教程)