Android 将图片网址url转化为bitmap,drawable转bitmap,file转bitmap,bitmap转file,Bitmap转String,Uri转Bitmap

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415543.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小程序 web-view 打开 微信公众号文章

首先微信小程序不能不能随便打开一个网址就能进行访问。 必须要配置业务域名。 但是&#xff0c;如果小程序和微信公众号进行了关联&#xff0c;则直接可以打开这个公众号到文章。 我已经测试过了。肯定好用。 还有一种方案&#xff0c;打开别人公众号的文章&#xff0c;你…

IP协议包中的TTL(Time-To-Live)

TTL(Time-To-Live)的作用是限制数据包在网络中存在的时间&#xff0c;防止数据包不断的在IP互联网络上循环。 TTL指定数据包被路由器丢弃之前允许通过的最大网段数量&#xff0c;是IP数据包在网络中可以转发的最大跳数(跃点数)&#xff0c;TTL位于IPv4包的第9个字节&#xff0c…

常用的方法论-PARR

转载于:https://www.cnblogs.com/qjm201000/p/7687470.html

小程序 自定义遮照 mask 禁止上下滑动

要写一个提示框&#xff0c;有灰度&#xff0c;放到正常页面最外层。发现上下滑动&#xff0c;里层的页面也会跟着上下动&#xff0c;其实只需要再设置一下page的属性就行了。 <style lang"less" scoped>.verify-mask {z-index: 99999;position: fixed;top: 0…

修改wireshark协议解析规则

不同的协议有不同的解码器,wireshark尝试为每个包尝试找到正确的解码器,特定的情况有可能会选择错误的解码器。 1.使用了其它协议的标准端口&#xff0c;被错误解码&#xff0c;使用udp的80端口发送数据被当作QUIC协议解析。 wireshark菜单“Analyze–>Enabled Protocols…”…

java 泛型类使用,集合中添加不同类型数据

添加 ArrayList<String> a new ArrayList<String>(); a.add("CSDN_SEU_Cavin"); Class c a.getClass(); try{Method method c.getMethod("add",Object.class);method.invoke(a,100);System.out.println("lgqname:" a); }catch(Ex…

小程序 遮照 mask 背景透明,里面内容不透明

1、错误的写法 .mask {background: black;opacity: 0.5; }2、正确的写法 .mask {background: rgba(0, 0, 0, 0.5); }

skt框架开发设计1

本系列文章将从零开始设计一套后台服务器框架&#xff0c;框架基于插件开发模式&#xff0c;最终各个插件相互独立&#xff0c;互相不依赖&#xff0c; 这样设计的好处是最终开发人员负责各个模块的开发&#xff0c;相互之间能够相对独立的进行&#xff1b;转载于:https://www.…

java集合数组,数组小到大排序,数组大到小排序

List<?>[] lsa new List<?>[10]; // OK, array of unbounded wildcard type. Object o lsa; Object[] oa (Object[]) o; List<Integer> li new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] li; // Correct. Integer i (Integer) lsa…

端口占用

查看本地端口占用时发现有三种不同类型的监听方法&#xff1a;0.0.0.0:3602、127.0.0.1:3602、192.168.0.3:3602&#xff0c;其中IP&#xff1a;0.0.0.0代表当前设备的所有IP地址&#xff0c;使用这种方法监听的端口后面两种ip都可以连接&#xff0c;但这个IP并不真实存在&…

小程序中 使用fixed自定义弹窗时,底部长页面禁止滚动

原生小程序中可以在弹层上使用catchtouchmove&#xff0c;此事件会阻止向父元素冒泡&#xff0c;mpvue中使用touchmove.stop"()>{}"。

select into from和insert into select

select into from 和 insert into select都是用来复制表&#xff0c;两者的主要区别为&#xff1a;select into from 要求目标表不存在&#xff0c;因为在插入时会自动创建&#xff1b;insert into select 要求目标表存在 insert into select语法&#xff1a; Insert into Tab…

Fiddler过滤指定域名

Fiddler过滤指定域名的方法一 切换到fiddler右侧窗口的Filters选项卡&#xff0c;勾选顶部的“Use Filters”&#xff0c;找到Hosts区域&#xff0c;设置以下三个选项&#xff1a; 1.第一项有三个选项&#xff0c;不做更改&#xff1a; “No zone filter” “Show Only Intrane…

hapi logs 日志保存代码片段

按照每个块的大小 reporters: {fileReporter: [{module: good-squeeze,name: Squeeze,args: [{ ops: * }],},{module: good-squeeze,name: SafeJson,args: [null,{ separator: , },],}, {module: rotating-file-stream,args: [ops_log,{size: 10MB,path: ./logs,},],},],error…

Android dp转px,sp转px

/*** 将dip或dp值转换为px值&#xff0c;保证尺寸大小不变*/ public static int dip2px(Context context, float dipValue) {final float scale context.getResources().getDisplayMetrics().density;return (int) (dipValue * scale 0.5f); }/*** 将sp值转换为px值&#xff…

求出100~200之间的素数

求出100~200之间的素数的个数&#xff0c;并求出所有的素数。 分析&#xff1a;素数定义是只能被1和该数本身整除 package com.math.forth;/*** 求出100~200之间的素数的个数&#xff0c;并求出所有的素数。 * 分析&#xff1a;素数定义是只能被1和该数本身整除* * author wql*…