适配所有机型的保存图片工具

来源:互联网 发布:数据可视化软件 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;}

}

0 0
原创粉丝点击