根据图片uri生成Bitmap对象
public static Bitmap decodeUriAsBitmap(Context context, Uri uri) {
if (uri == null) return null;
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
bitmap 转base64
public static String getBitmapBase64String(Bitmap bitmap) {
ByteArrayOutputStream out = new ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
try {
out.flush()
out.close()
} catch (IOException e) {
e.printStackTrace()
}
return Base64.encodeToString(out.toByteArray(), Base64.DEFAULT)
}
base64转成bitmap
public static Bitmap StringToBitmap(String st) {
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(st, Base64.DEFAULT);
bitmap =
BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
return bitmap;
} catch (Exception e) {
return null;
}
}
本地图片路径String转base64
public static String getFileBase64String(String path) {
File file = new File(path);
try {
FileInputStream is = new FileInputStream(file);
ByteArrayOutputStream out= new ByteArrayOutputStream();
int i = is.read();
while (i != -1) {
out.write(i);
i= is.read();
}
return Base64.encodeToString(out.toByteArray(), Base64.DEFAULT);
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
加载本地图片
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
从服务器取图片
public static Bitmap getHttpBitmap(final String url) {
class MyThread extends Thread {
@Override
public void run() {
try {
myFileUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MyThread thread = new MyThread();
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
return bitmap;
}
将Bitmap保存在本地
public static void saveBitmap(Context context, Bitmap bitmap) {
File appDir = new File(Environment.getExternalStorageDirectory(),
"BrotherRepair");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = "BrotherRepair" + ".png";
File file = new File(appDir, fileName);
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();
}
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + "/sdcard/namecard/")));
}
bitmap转换成byte数组
/**
* bitmap转换成byte数组
*
* @param bitmap
* @param needRecycle
* @return
*/
public static byte[] bitmapToByteArray(Bitmap bitmap, boolean needRecycle) {
if (null == bitmap) {
return null;
}
if (bitmap.isRecycled()) {
return null;
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
if (needRecycle) {
bitmap.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
}
return result;
}