首页 > 代码库 > android 存储图片到data目录和读取data目录下的图片
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();
读取图片代码:
[java] view plain copy
- String localIconNormal = "icon.png";
- FileInputStream localStream = openFileInput(localIconNormal);
- Bitmap bitmap = BitmapFactory.decodeStream(localStream));
android 存储图片到data目录和读取data目录下的图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。