导航:首页 > 操作系统 > android分享bitmap

android分享bitmap

发布时间:2022-09-02 04:57:45

android,如何读取资源文件里的图片到bitmap里

  1. 方式:以R文件的方式

  2. 路径:假设 res/drawable下有 test.jpg文件

    Bitmap bitmap=BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test)

⑵ android bitmap 使用时候注意什么

一、 问题的背景和意义
在Android移动应用开发中,对Bitmap的不小心处理,很容易引起程序内存空间耗尽而导致的程序崩溃问题。比如我们常遇到的问题:
java.lang.OutofMemoryError: bitmap size exceeds VM budget.
导致该问题的出现,一般由以下几方面原因导致:
引动设备一般存储空间非常有限。当然不同设备分配给应用的内存空间是不同的。但相对不但提高的设备分辨率而言,内存的分配仍然是相对紧张的。
Bitmap对象常常占用大量的内存空间,比如:对于2592*1936的设备,如果采用ARGB_8888的格式加载图像,内存占用将达到19MB空间。
在Anroid App中经常用到ListView,ViewPager等控件,这些控件常会包含较大数量的图片资源。
二、 问题及场景分析
1 高效地加载大图片。
BitmapFactory类提供了一些加载图片的方法:decodeByteArray(), decodeFile(), decodeResource(), 等等。
为了避免占用较大内存,经常使用BitmapFactory.Options 类,设置inJustDecodeBounds属性为true。
//
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds =true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
为了避免java.lang.OutOfMemory 的异常,我们在真正decode图片之前检查它的尺寸,除非你确定这个数据源提供了准确无误的图片且不会导致占用过多的内存。
加载一个按比例缩小的版本到内存中。例如,如果把一个原图是1024*768 pixel的图片显示到ImageView为128*96 pixel的缩略图就没有必要把整张图片都加载到内存中。为了告诉解码器去加载一个较小的图片到内存,需要在你的BitmapFactory.Options 中设置 inSampleSize 为true 。例如, 一个分辨率为2048x1536 的图片,如果设置inSampleSize 为4,那么会产出一个大概为512x384的图片。加载这张小的图片仅仅使用大概0.75MB,如果是加载全图那么大概要花费12MB(假设bitmap的配置是ARGB_8888).
2 不要在主线程处理图片。

众所周知的问题,不再赘述。
注意两点:1. 为了保证使用的资源能被回收,建议使用WeakReference, 以应用内存内存紧张时,回收部分资源,保证程序进程不被杀死。
2. 避免异步任务的长时间耗时操作,在任务执行结束后,及时释放资源。
3 管理Bitmap内存。
在Android开发中,加载一个图片到界面很容易,但如果一次加载大量图片就复杂多了。在很多情况下(比如:ListView,GridView或ViewPager),能够滚动的组件需要加载的图片几乎是无限多的。
有些组件的child view在不显示时会回收,并循环使用,如果没有任何对bitmap的持久引用的话,垃圾回收器会释放你加载的bitmap。这没什么问题,但当这些图片再次显示的时候,要想避免重复处理这些图片,从而达到加载流畅的效果,就要使用内存缓存和本地缓存了,这些缓存可以让你快速加载处理过的图片。
3.1 内存缓存
内存缓存以牺牲内存的代价,带来快速的图片访问。LruCache类(API Level 4之前可以使用Support Library)非常适合图片缓存任务,在一个LinkedHashMap中保存着对Bitmap的强引用,当缓存数量超过容器容量时,删除最近最少使用的成员(LRU)。
注意:在过去,非常流行用SoftReference或WeakReference来实现图片的内存缓存,但现在不再推荐使用这个方法了。因为从Android 2.3 (API Level 9)之后,垃圾回收器会更积极的回收soft/weak的引用,这将导致使用soft/weak引用的缓存几乎没有缓存效果。顺带一提,在Android3.0(API Level 11)以前,bitmap是储存在native 内存中的,所以系统以不可预见的方式来释放bitmap,这可能会导致短时间超过内存限制从而造成崩溃。 收起

⑶ android 如何实现bitmap分享到QQ空调

public class AndroidShare {
/**
* 上下文
*/
private Context context;
/**
* 文本类型
*
*/
public static int TEXT = 0;
/**
* 图片类型
*/
public static int DRAWABLE = 1;
public AndroidShare(Context context) {
this.context = context;
}
/**
* 分享到QQ好友
*
* @param msgTitle
* (分享标题)
* @param msgText
* (分享内容)
* @param type
* (分享类型)
* @param drawable
* (分享图片,若分享类型为AndroidShare.TEXT,则可以为null)
*/
public void shareQQFriend(String msgTitle, String msgText, int type,
Bitmap drawable) {
shareMsg("com.tencent.mobileqq",
"com.tencent.mobileqq.activity.JumpActivity", "QQ", msgTitle,
msgText, type, drawable);
}
/**
* 分享到微信好友
*
* @param msgTitle
* (分享标题)
* @param msgText
* (分享内容)
* @param type
* (分享类型)
* @param drawable
* (分享图片,若分享类型为AndroidShare.TEXT,则可以为null)
*/
public void shareWeChatFriend(String msgTitle, String msgText, int type,
Bitmap drawable) {
shareMsg("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", "微信",
msgTitle, msgText, type, drawable);
}
/**
* 分享到微信朋友圈(分享朋友圈一定需要图片)
*
* @param msgTitle
* (分享标题)
* @param msgText
* (分享内容)
* @param drawable
* (分享图片)
*/
public void shareWeChatFriendCircle(String msgTitle, String msgText,
Bitmap drawable) {
shareMsg("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI",
"微信", msgTitle, msgText, AndroidShare.DRAWABLE, drawable);
}
/**
* 点击分享的代码
*
* @param packageName
* (包名,跳转的应用的包名)
* @param activityName
* (类名,跳转的页面名称)
* @param appname
* (应用名,跳转到的应用名称)
* @param msgTitle
* (标题)
* @param msgText
* (内容)
* @param type
* (发送类型:text or pic 微信朋友圈只支持pic)
*/
@SuppressLint("NewApi")
private void shareMsg(String packageName, String activityName,
String appname, String msgTitle, String msgText, int type,
Bitmap drawable) {
if (!packageName.isEmpty() && !isAvilible(context, packageName)) {// 判断APP是否存在
Toast.makeText(context, "请先安装" + appname, Toast.LENGTH_SHORT)
.show();
return;
}
Intent intent = new Intent("android.intent.action.SEND");
if (type == AndroidShare.TEXT) {
intent.setType("text/plain");
} else if (type == AndroidShare.DRAWABLE) {
intent.setType("image/*");
// BitmapDrawable bd = (BitmapDrawable) drawable;
// Bitmap bt = bd.getBitmap();
final Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
context.getContentResolver(), drawable, null, null));
intent.putExtra(Intent.EXTRA_STREAM, uri);
}
intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
intent.putExtra(Intent.EXTRA_TEXT, msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (!packageName.isEmpty()) {
intent.setComponent(new ComponentName(packageName, activityName));
context.startActivity(intent);
} else {
context.startActivity(Intent.createChooser(intent, msgTitle));
}
}
/**
* 判断相对应的APP是否存在
*
* @param context
* @param packageName
* @return
*/
public boolean isAvilible(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
for (int i = 0; i < pinfo.size(); i++) {
if (((PackageInfo) pinfo.get(i)).packageName
.equalsIgnoreCase(packageName))
return true;
}
return false;
}
/**
* 指定分享到qq
* @param context
* @param bitmap
*/
public void sharedQQ(Activity context,Bitmap bitmap){
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
context.getContentResolver(), BitmapFactory.decodeResource(context.getResources(), R.mipmap.company_mainback), null, null));
Intent imageIntent = new Intent(Intent.ACTION_SEND);
imageIntent.setPackage("com.tencent.mobileqq");
imageIntent.setType("image/*");
imageIntent.putExtra(Intent.EXTRA_STREAM, uri);
imageIntent.putExtra(Intent.EXTRA_TEXT,"您的好友邀请您进入天好圈");
imageIntent.putExtra(Intent.EXTRA_TITLE,"天好圈");
context.startActivity(imageIntent);
}
}

⑷ android开发怎么得到Bitmap所占资源的大小

⑸ android 怎么上传bitmap 到数据库

andorid推荐的是如果数据在20-50k可以存放到数据库,否则数据库存路径。

⑹ android bundle能传递bitmap吗

可以啊 可以直接传好吧!
Bitmap bitmap = null;
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
intent.putExtra("bundle", bundle);
bitmap已经自己实现了Parcelable接口

⑺ android怎么生成bitmap

1、

[java] view plain
public Bitmap convertViewToBitmap(View view){

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
//利用bitmap生成画布
Canvas canvas = new Canvas(bitmap);

//把view中的内容绘制在画布上
view.draw(canvas);

return bitmap;
}

2、

[java] view plain
/**
* save view as a bitmap
*/
private Bitmap saveViewBitmap(View view) {
// get current view bitmap
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache(true);

Bitmap bmp = plicateBitmap(bitmap);
if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; }
// clear the cache
view.setDrawingCacheEnabled(false);
return bmp;
}

public static Bitmap plicateBitmap(Bitmap bmpSrc)
{
if (null == bmpSrc)
{ return null; }

int bmpSrcWidth = bmpSrc.getWidth();
int bmpSrcHeight = bmpSrc.getHeight();

Bitmap bmpDest = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888); if (null != bmpDest) { Canvas canvas = new Canvas(bmpDest); final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);

canvas.drawBitmap(bmpSrc, rect, rect, null); }

return bmpDest;
}

⑻ android 怎么获得bitmap对象

保存界面可视化组件,在调用View.getDrawingCache方法前需要调用measure和layout方法才能可以成功获取布局的Bitmap对象
代码如下:

//获得xml的布局截图
View view=getLayoutInflate r().inflate(R.layout.dialog, null);
//打开图像缓存
view.setDrawingCacheEnabled(true);
//需要调用measure和layout方法
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//发送位置和尺寸到view及其所有的子view
view.layout(0, 0, view.getMeasuredWidth(),view.getMeasuredHeight());
//获得可视化组件的截图
Bitmap bitmap=view.getDrawingCache();
try {
//保存在sdcard目录下
FileOutputStream fos=new FileOutputStream("/sdcard/test.png");
bitmap.compress(CompressFormat.PNG, 100, fos);
//关闭文件输出流
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

⑼ android 读取Bitmap的几种方式

想读取本地项目里的资源图片,但又不能用到R文件。查了很多资料终于找到了。 现总结以下几种读取Bitmap的方法。 1.以文件流的方式,假设在sdcard下有 test.png图片 FileInputStream fis = new FileInputStream("/sdcard/test.png"); Bitmap bitmap=BitmapFactory.decodeStream(fis); 2. 以R文件的方式,假设 res/drawable下有 test.jpg文件Bitmapbitmap =BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test); 3.以ResourceStream的方式,但不用到R文件。 Bitmap.bitmap=BitmapFactory.decodeStream(getClass().getResourceAsStream(“/res/drawable/test.png”)); 图片名就可以读取到 Bitmap啦。 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; //图片宽高都为原来的二分之一,即图片为原来的四分一 //以上代码可以优化内存溢出,但它只是改变图片大小,并不能彻底解决内存溢出。

⑽ android 自带的分享功能如何实现分享图片


bgimg0=getImageFromAssetsFile("Cat_Blink/cat_blink0000.png");

/**
*从Assets中读取图片
*/
(StringfileName)
{
Bitmapimage=null;
AssetManageram=getResources().getAssets();
try
{
InputStreamis=am.open(fileName);
image=BitmapFactory.decodeStream(is);
is.close();
}
catch(IOExceptione)
{
e.printStackTrace();
}

returnimage;

}

上面的代码是从assets中获取图片的代码,下面的代码是分享图片的代码:

/**
*分享功能
*
*@paramcontext
*上下文
*@paramactivityTitle
*Activity的名字
*@parammsgTitle
*消息标题
*@parammsgText
*消息内容
*@paramimgPath
*图片路径,不分享图片则传null
*/
publicvoidshareMsg(StringactivityTitle,StringmsgTitle,StringmsgText,
StringimgPath){
Intentintent=newIntent(Intent.ACTION_SEND);
if(imgPath==null||imgPath.equals("")){
intent.setType("text/plain");//纯文本
}else{
Filef=newFile(imgPath);
if(f!=null&&f.exists()&&f.isFile()){
intent.setType("image/jpg");
Uriu=Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM,u);
}
}
intent.putExtra(Intent.EXTRA_SUBJECT,msgTitle);
intent.putExtra(Intent.EXTRA_TEXT,msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent,activityTitle));
}

系统的分享,你想要分享图片需要先把图片存到本地才能分享

阅读全文

与android分享bitmap相关的资料

热点内容
程序员在广州上班 浏览:800
androidlinuxadt 浏览:510
广联达软件加密锁原装芯片 浏览:338
如何打开数据库服务器 浏览:310
kppm是什么app 浏览:538
python多个数组命名 浏览:191
a算法csdn 浏览:23
r720服务器什么年代 浏览:975
本地电脑怎么设置传奇服务器 浏览:1002
安卓10框架怎么制作 浏览:959
程序员退休工资待遇 浏览:609
湛江中文编程数控系统代理 浏览:419
openglandroid书 浏览:170
奇妙组件安卓版叫什么 浏览:729
微信授权什么app权重最高 浏览:11
php循环数组foreach 浏览:78
zip和app有什么区别 浏览:633
乖法快速算法 浏览:872
日本程序员一年工资 浏览:199
出国做程序员怎么样 浏览:736