zoukankan      html  css  js  c++  java
  • android 存储图片到data目录和读取data目录下的图片


    public void storePic(String tabid, String key, Bitmap bitmap) {
    LogUtils.LOGD(TAG, "storePic begin tabid = " + tabid + "key = " + key);
    if(tabid == null || key == null || tabid.isEmpty() || key.isEmpty() || bitmap == null) {
    return;
    }
    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);
    if(tabid == null || key == null || tabid.isEmpty() || key.isEmpty()) {
    return null;
    }
    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
    1. String str1 = "icon.png";  
    2.   
    3. FileOutputStream localFileOutputStream1 = openFileOutput(str1, 0);  
    4.   
    5. Bitmap.CompressFormat localCompressFormat = Bitmap.CompressFormat.PNG;  
    6.   
    7. bitmap.compress(localCompressFormat, 100, localFileOutputStream1);  
    8.   
    9. localFileOutputStream1.close();  

    读取图片代码:

    [java] view plain copy
    1. String localIconNormal = "icon.png";  
    2.   
    3. FileInputStream localStream = openFileInput(localIconNormal);  
    4.   
    5. Bitmap bitmap = BitmapFactory.decodeStream(localStream));  
  • 相关阅读:
    暑期大作战第三天
    暑期大作战 第二天
    暑假作战第一天
    JDK源码学习笔记——Object
    JVM堆 栈 方法区详解
    JVM入门——JVM内存结构
    Spring Boot 1.Hello World
    Flutter Widget不刷新问题
    Flutter 根界面退出的时候(即是应用退出),不会触发deactivate/dispose方法 / 监听返回按钮
    Android Studio 3.3.1 代码提示不区分大小写
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/6180762.html
Copyright © 2011-2022 走看看