1.拷贝图片的工具类
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator  .
 */
public class BitmapUtil {
    private static final String DEAFAULT_FILE_PATH = getSdCardPath() + File.separator;

    private BitmapUtil() {
    }

    /**
     * 以最省内存的方式读取本地资源的图片
     *
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

        Bitmap bmp = BitmapUtil.getDiskBitmap(path);
        if(bmp != null) {
            mHeaderIv.setImageBitmap(bmp);
        }
    /**
     * 读取本地图片
     * @param path 图片路径
     * */
    public static Bitmap getDiskBitmap(String path) {
        Bitmap bitmap = null;
        if(TextUtils.isEmpty(path)) {
            return bitmap;
        }
        try {
            File file = new File(path);
            if (file.exists()) {
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inPreferredConfig = Bitmap.Config.RGB_565;
                opt.inPurgeable = true;
                opt.inInputShareable = true;
                bitmap = BitmapFactory.decodeFile(path, opt);
             }
        } catch (Exception e) {
        }

        return bitmap;
    }

  
    /**
     * 保存图片到本地 第一个参数是图片名称 第二个参数为需要保存的bitmap
     * */
    public static void saveImgToDisk(String imgName, Bitmap bitmap) {
        File file = new File(DEAFAULT_FILE_PATH, imgName);
        if(file == null) {
            return;
        }

        if(isFileExists(file.getPath())) {
            return;
        }


        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            String path = file.getPath();
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断文件路径是否已经存在
     * @param filePath 文件路径
     * */
    private static boolean isFileExists(String filePath) {
        try {
            File file = new File( filePath );
            return file.exists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    /**
     * 返回SD卡跟目录

     *
     * @return SD卡跟目录
     */
    public static String getSdCardPath() {
        File sdDir ;
        boolean sdCardExist = isSdCardExist(); // 判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
            return sdDir.toString();
        } else {
            return null;
        }
    }

    /**
     * 判断SD卡是否存在

     *
     * @return SD卡存在返回true,否则返回false
     */
    public static boolean isSdCardExist() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
}


2.读取本地图片
 Bitmap bmp = BitmapUtil.getDiskBitmap(path);
 if(bmp != null) {
    mHeaderIv.setImageBitmap(bmp);
 }

 
3.保存图片到本地
 Bitmap bmp = BitmapFactory.decodeFile(localPath);
 saveImgToDisk(fileName, bmp);

更多相关文章

  1. android ftp服务器实现
  2. Android(安卓)Contacts(一)—— 读取联系人
  3. android 关于图片的放大,缩小,旋转功能的实现(附源码)
  4. Android(安卓)Location 和 Sensor的使用,图片旋转角度
  5. 读取Android设备的MAC地址
  6. 调用Android摄像头与打开相册
  7. 将ffmpeg编译到android 源码
  8. android的编译和运行过程深入分析
  9. Android中使用ALSA声卡

随机推荐

  1. activity标签总结2
  2. 【解决方法】ADT在线安装
  3. Android之WebView 防止调用系统浏览器打
  4. android样式跟主题
  5. Android(安卓)ListView几个比较有用的属
  6. android mediaStore
  7. 2022讲师招募、课程合作计划(php中文网)
  8. 新手小白sai如何画线稿?初学者绘制线稿步
  9. 【北亚数据恢复】oracle数据库执行trunca
  10. 实践:Linux下安装mysql8.0