/**
* 从sdcard获取图片转换为bitmap或drawable
* @param path
* @return
*/
@SuppressWarnings("deprecation")
public static Drawable stringToDrawable(String path){
try {
FileInputStream input = new FileInputStream(new File(path));
Bitmap bmp = BitmapFactory.decodeStream(input);
input.close();
Drawable drawable = new BitmapDrawable(bmp);
return drawable;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 从assets文件夹获取图片转换为bitmap或drawable
* @param ctx
* @param fileName
* @return
*/
String path = "images/" + "a/" + "b.png";
@SuppressWarnings("deprecation")
public static Drawable getImageFromAssetsFile(Context ctx,String fileName){
AssetManager am = ctx.getResources().getAssets();
try{
InputStream input = am.open(fileName);
Bitmap bmp = BitmapFactory.decodeStream(input);
input.close();
Drawable drawable = new BitmapDrawable(bmp);
return drawable;
}catch (IOException e){
e.printStackTrace();
}
return null;
}
/**
* 保存图片到sdcard,不做压缩
* @param bitmap
* @return
*/
public static String storeInSD(Bitmap bitmap) {
String path = Environment.getExternalStorageDirectory() + File.separator + "Test";
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
File imageFile = new File(file, System.currentTimeMillis() + ".jpg");
try {
imageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(imageFile);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
return imageFile.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 保存string到test.xml到sdcard
* @param context
* @param fileName "test.xml" /data/data/com.test.activity/files/test.xml
* @param txt
* @return
*/
public static boolean storeInSD(Context context,String fileName,String txt) {
try {
FileOutputStream os = context.openFileOutput(fileName,Context.MODE_PRIVATE);
os.write(txt.getBytes("UTF-8"));
os.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 从文件夹读xml文件
* @param path "/data/data/com.test.activity/files/test.xml"
* @return
*/
public static ArrayList<MyTrackPoints> readSdXML(String path){
File file = new File(path);
try {
if(!file.exists()){
file.createNewFile();
}
InputStream input = new FileInputStream(file);
ArrayList<MyTrackPoints> list = MyXml.parserXml(input);
return list;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 判断sdcard是否存在
* @return
*/
public static boolean isHasSdcard() {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
/**
* 判断文件是否存在
* @param dir
* @return
*/
public static boolean isFileExists(String dir) {
File destDir = new File(dir);
if (!destDir.exists()) {
destDir.mkdirs();
return true;
} else {
return false;
}
}
/**获取当前格式化时间
* get cur time
*/
@SuppressLint("SimpleDateFormat")
public static String getCurTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
return formatter.format(curDate);
}
/**删除文件
* delete file
* @param filePath
* @return
*/
public boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
return file.delete();
}
return false;
}