适配所有机型的保存图片工具
来源:互联网 发布:数据可视化软件 ta 编辑:程序博客网 时间:2024/06/05 03:27
/**
* Created by liuzongxin on 2015/11/18.
*/
public class BitmapUtil {
public static void saveBitmap(Context context, Bitmap bitmap){
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(“‘IMG’_yyyyMMdd_HHmmss”);
String fileName = dateFormat.format(date) + “.jpg”;
String path = getExternalSdCardPath(context) + “/mingyi/pics/”;//保存的image的根目录
if(!(path == null || path.length() == 0 || path.equals(“”))){
File fileDir = new File(path);
if(!fileDir.exists()){
fileDir.mkdir();
}
File file = new File(path , fileName);
//设置最后保存时间,有些手机不设置会显示1970年
long time=System.currentTimeMillis();
file.setLastModified(time);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 其次把文件插入到系统图库
ContentValues values = new ContentValues();
values.put(“datetaken”, new Date().toString());
values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, time);
values.put(MediaStore.Images.ImageColumns.DATE_ADDED, time/1000);
values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, time/1000);
values.put(“mime_type”, “image/jpg”);
values.put(“_data”, file.getAbsolutePath());
ContentResolver cr = context.getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
MediaScannerConnection.scanFile(context, new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()
+ “/” + file.getParentFile().getAbsolutePath()}, null, null);
Toast.makeText(context , “图片已保存至”+file.getAbsolutePath() , Toast.LENGTH_SHORT ).show();
}else{ //保存失败 Toast.makeText(context , "保存失败!" , Toast.LENGTH_SHORT ).show();
// return null;
}
}
public static String getExternalSdCardPath(Context context){
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
return sdCardFile.getAbsolutePath();
}else{
if(context != null){
Toast.makeText(context, “SD卡不可用” , Toast.LENGTH_SHORT).show();
}
}
String path = null; File sdCardFile = null; ArrayList<String> devMountList = getDevMountList(); if(devMountList != null){ for (String devMount : devMountList) { File file = new File(devMount); if (file.isDirectory() && file.canWrite()) { path = file.getAbsolutePath(); String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date()); File testWritable = new File(path, "test_" + timeStamp); if (testWritable.mkdirs()) { testWritable.delete(); } else { path = null; } } } } if (path != null) { sdCardFile = new File(path); return sdCardFile.getAbsolutePath(); } return null ;}private static ArrayList<String> getDevMountList() { String[] toSearch; ArrayList<String> out = null; try { toSearch = readSDFile("/etc/vold.fstab").split(" "); out = new ArrayList<String>(); for (int i = 0; i < toSearch.length; i++) { if (toSearch[i].contains("dev_mount")) { if (new File(toSearch[i + 2]).exists()) { out.add(toSearch[i + 2]); } } } } catch (IOException e) { e.printStackTrace(); } return out;}private static String readSDFile(String fileName) throws IOException { String res = "" ; File file = new File(fileName); FileInputStream fis = new FileInputStream(file); int length = fis.available(); byte [] buffer = new byte[length]; fis.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fis.close(); return res;}
}
- 适配所有机型的保存图片工具
- Android 机型适配-图片或者视频保存到相册立即显示问题
- 关于iphone各机型图片的适配
- iphone图标制作,适配所有机型的分辨率的icon制作网站
- H5 页面适配所有 iPhone 和安卓机型的六个技巧
- 安卓微信客户端背景图无法100%适配所有 机型的问题
- H5 页面适配所有 iPhone 和安卓机型的六个技巧
- H5 页面适配所有 iPhone 和安卓机型的六个技巧
- H5 页面适配所有 iPhone 和安卓机型的六个技巧
- H5 页面适配所有 iPhone 和安卓机型的六个技巧
- 关于xcode6新特性size class 适配所有机型
- cocos2d-x 一套资源适配所有机型 教程(一)
- 用一张图片适应所有机型的方法,但会出现失真的情况
- 提取所有mtk机型的线刷包
- Macmini 所有机型的【待机、工作】 功率
- 保存图片文件的工具类
- 机型适配的一点小问题
- 机型适配的一些小技巧
- jsp页面防止刷新重复提交解决方案
- 常用js特效之左上角菜单
- 页面设计
- 用php调用接口api
- 关于scrollview嵌套listview的方法。但官方并不推荐这种做法。
- 适配所有机型的保存图片工具
- Flex 字符串拼接、去掉字符串两端空字符
- delphi idHttp下载文件
- windows下apache+php+mysql 环境配置方法
- poj 2184 Cow Exhibition 01背包
- ps如何制作霓虹字体
- java获取最新地区
- iOS滤镜效果的实现:CoreImage
- 判断字符串是否包含某些字符