Android 中读取图片方式分享

来源:互联网 发布:微信里淘宝链接 编辑:程序博客网 时间:2024/05/18 12:41

Android 中读取图片方式分享


一:读取res中的图片//读取本地res中的图片public static Bitmap readBitmap(int resid){<span style="white-space:pre"></span>BitmapFactory.Options opt = new BitmapFactory.Options();<span style="white-space:pre"></span>opt.inPreferredConfig = Bitmap.Config.RGB_565;<span style="white-space:pre"></span>opt.inPurgeable = true;<span style="white-space:pre"></span>opt.inInputShareable = true;<span style="white-space:pre"></span>//获取资源图片<span style="white-space:pre"></span>InputStream input = getResources().openRawResource(resid);<span style="white-space:pre"></span>return BitmapFactory.decodeStream(input,null,opt);}<span style="white-space:pre"></span>//确定不再需要该bitmap对象的时候可以将其回收掉<span style="white-space:pre"></span>public static void recycle(Bitmap bitmap){<span style="white-space:pre"></span>if(bitmap!=null && !bitmap.isRecycle()){<span style="white-space:pre"></span>bitmap.recycle();<span style="white-space:pre"></span>System.gc();//提醒系统及时回收     }}二:放在assets中的图片(只读)<span style="white-space:pre"></span>InputStream is = context.getResources().getAssets().open("icon.png");<span style="white-space:pre"></span>Bitmap bitmap = BitmapFactory.decodeStream(is);<span style="white-space:pre"></span>三:读取sd卡中的图片<span style="white-space:pre"></span>Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/icon.png");<span style="white-space:pre"></span>四:读取网络图片<span style="white-space:pre"></span>public static Bitmap loadImageFromUrl(String urlStr){<span style="white-space:pre"></span>URL url;<span style="white-space:pre"></span>InputStream i = null;<span style="white-space:pre"></span>try{<span style="white-space:pre"></span>m = new URL(urlStr);<span style="white-space:pre"></span>i = m.getContent();<span style="white-space:pre"></span>}catch(Exception e){<span style="white-space:pre"></span>e.printStaceTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return BitmapFactory.decodeStream(i);}


0 0