適配所有機型的保存圖片工具

/**
* 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;
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章