附各种转换:
http://glblong.blog.51cto.com/3058613/1304090
这里要申明权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
bitmap转本地URL:
public static String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
// 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
}
return sdDir.toString();
}
/**
* 保存文件
*
* @param bm
* @param fileName
* @throws IOException
*/
public void saveFile(Bitmap bm, String fileName) throws IOException {
//获得路径
String path = getSDPath() + "/revoeye/";
File dirFile = new File(path);
if (!dirFile.exists()) {
dirFile.mkdirs();//保存路径
}
// 这里最好不要保存的名字都一样,容易出现imageLoader缓存的问题
//
path + fileName + ".jpg" 就是本地的URLFile myCaptureFile = new File(path + fileName + ".jpg");
if(myCaptureFile.exists()){
myCaptureFile.delete();
}
myCaptureFile.createNewFile();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
}
本地URL转Bitmap:
/**
缩小图片到width和height的范围内*
* @param path 图片的位置
* @param width 宽度
* @param height 高度
* @return
*/
public static Bitmap resizePhoto(String path, int width, int height) {
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(new File(path)));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
int i = 0;
Bitmap bitmap = null;
while (true) {
if ((options.outWidth >> i <= width) && (options.outHeight >> i <= height)) {
in = new BufferedInputStream(new FileInputStream(new File(path)));
options.inSampleSize = (int) Math.pow(2.0D, i);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(in, null, options);
break;
}
i += 1;
}
return bitmap;
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
// ignore
}
}
return null;
}
Bitmap转为本地URL并存在Card && 本地URL转Bitmap
时间: 2024-12-19 20:01:33