file转bitmap
File param = new File();
Bitmap bitmap= BitmapFactory.decodeFile(param.getPath());
drawable转bitmap
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.mipmap.jcss_03 );
url转bitmap
Bitmap bitmap;
public Bitmap returnBitMap(final String url){new Thread(new Runnable() {@Overridepublic void run() {URL imageurl = null;try {imageurl = new URL(url);} catch (MalformedURLException e) {e.printStackTrace();}try {HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();conn.setDoInput(true);conn.connect();InputStream is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();} catch (IOException e) {e.printStackTrace();}}}).start();return bitmap;
}
方法二:
public Bitmap getBitmap(String url) {Bitmap bm = null;try {URL iconUrl = new URL(url);URLConnection conn = iconUrl.openConnection();HttpURLConnection http = (HttpURLConnection) conn;int length = http.getContentLength();conn.connect();// 获得图像的字符流InputStream is = conn.getInputStream();BufferedInputStream bis = new BufferedInputStream(is, length);bm = BitmapFactory.decodeStream(bis);bis.close();is.close();// 关闭流}catch (Exception e) {e.printStackTrace();}return bm; }
可配合前台线程显示
private Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case REFRESH_COMPLETE:myheadimage.setImageBitmap(bitmap);//显示break;}}
};
String imageUrl = "http://www.pp3.cn/uploads/201511/2015111212.jpg";
bitmap= returnBitMap(imageUrl);
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 1000);
bitmap转file
private String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)? Environment.getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";//private String SAVE_REAL_PATH = SAVE_PIC_PATH + "/good/savePic";//保存的确
saveFile(bmp, System.currentTimeMillis() + ".png");
//保存方法private void saveFile(Bitmap bm, String fileName) throws IOException {String subForder = SAVE_REAL_PATH;File foder = new File(subForder);if (!foder.exists()) foder.mkdirs();File myCaptureFile = new File(subForder, fileName);Log.e("lgq","图片保持。。。。wwww。。。。"+myCaptureFile);ends = myCaptureFile.getPath();if (!myCaptureFile.exists()) myCaptureFile.createNewFile();BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);bos.flush();bos.close();
// ToastUtil.showSuccess(getApplicationContext(), "已保存在/good/savePic目录下", Toast.LENGTH_SHORT);//发送广播通知系统Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri = Uri.fromFile(myCaptureFile);intent.setData(uri);this.sendBroadcast(intent);}
Uri转Bitmap
public static Bitmap decodeUri(Context context, Uri uri, int maxWidth, int maxHeight) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true; //只读取图片尺寸readBitmapScale(context, uri, options);//计算实际缩放比例int scale = 1;for (int i = 0; i < Integer.MAX_VALUE; i++) {if ((options.outWidth / scale > maxWidth &&options.outWidth / scale > maxWidth * 1.4) ||(options.outHeight / scale > maxHeight &&options.outHeight / scale > maxHeight * 1.4)) {scale++;} else {break;}}options.inSampleSize = scale;options.inJustDecodeBounds = false;//读取图片内容options.inPreferredConfig = Bitmap.Config.RGB_565; //根据情况进行修改Bitmap bitmap = null;try {bitmap = readBitmapData(context, uri, options);} catch (Throwable e) {e.printStackTrace();}return bitmap; }
bitmap与byte[]之间相互转换
Android 图片压缩,bitmap与byte[]之间相互转换,Bitmap转String:Android 图片压缩,Bitmap旋转,bitmap与byte[]之间相互转换,Bitmap与String互转_meixi_android的博客-CSDN博客_android mipmap转bitmap
bug在线交流:扣Q 1085220040