zoukankan      html  css  js  c++  java
  • Load bitmap from file Android

    
    
    public static Bitmap loadFromFile(String filename) {
          try {
              File f = new File(filename);
              if (!f.exists()) { return null; }
              Bitmap tmp = BitmapFactory.decodeFile(filename);
              return tmp;
          } catch (Exception e) {
              return null;
          }
      }
    
    
    
    import java.io.File;
    import java.io.FileOutputStream;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Bitmap.CompressFormat;
    import android.os.Environment;
    
    class PictUtil {
      public static File getSavePath() {
          File path;
          if (hasSDCard()) { // SD card
              path = new File(getSDCardPath() + "/Tegaky/");
              path.mkdir();
          } else { 
              path = Environment.getDataDirectory();
          }
          return path;
      }
      public static String getCacheFilename() {
          File f = getSavePath();
          return f.getAbsolutePath() + "/cache.png";
      }
    
      public static Bitmap loadFromFile(String filename) {
          try {
              File f = new File(filename);
              if (!f.exists()) { return null; }
              Bitmap tmp = BitmapFactory.decodeFile(filename);
              return tmp;
          } catch (Exception e) {
              return null;
          }
      }
      public static Bitmap loadFromCacheFile() {
          return loadFromFile(getCacheFilename());
      }
      public static void saveToCacheFile(Bitmap bmp) {
          saveToFile(getCacheFilename(),bmp);
      }
      public static void saveToFile(String filename,Bitmap bmp) {
          try {
              FileOutputStream out = new FileOutputStream(filename);
              bmp.compress(CompressFormat.PNG, 100, out);
              out.flush();
              out.close();
          } catch(Exception e) {}
      }
    
      public static boolean hasSDCard() { // SD????????
          String status = Environment.getExternalStorageState();
          return status.equals(Environment.MEDIA_MOUNTED);
      }
      public static String getSDCardPath() {
          File path = Environment.getExternalStorageDirectory();
          return path.getAbsolutePath();
      }
    
    }

     Now question comes,about the "Bitmap tmp = BitmapFactory.decodeFile(filename);",no matter how I modify it ,the result tmp is always null!
    the main point is options,the options should be specifid,indicate the format of the image.So it should be like this:

    BitmapFactory.Options opts = new Options();
    opts.inPreferredConfig = Config.RGB_565;
    tmp=BitmapFactory.decodeFile(bitmapPath,opts);
    
    
  • 相关阅读:
    docker 加速器配置目录
    php 超时设置笔记
    php socket通过smtp发送邮件(纯文本、HTML,多收件人,多抄送,多密送)
    fabric 安装
    centos7下使用yum安装pip
    【转】linux tar 压缩
    ASP.NET MVC 5 默认模板的JS和CSS 是怎么加载的?
    NHibernate with ASP.NET MVC 入门示例
    Ajax入门
    NHibernate入门
  • 原文地址:https://www.cnblogs.com/qiengo/p/2554572.html
Copyright © 2011-2022 走看看