80,351
社区成员
发帖
与我相关
我的任务
分享
方法一
/*
drawableSharePic = loadImageFromNetwork("reg","http://www.xxx.com/Android/shareImg/share_deal.jpg");
BitmapDrawable bd = (BitmapDrawable) drawableSharePic;
Bitmap thumb = bd.getBitmap();
msg.setThumbImage(thumb); //= Util.bmpToByteArray(thumb, true);
private Drawable loadImageFromNetwork(String type,String imageUrl)
{
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(new URL(imageUrl).openStream(), "share_reg.jpg");
} catch (IOException e) {
}
if (drawable == null) {
//如果没有成功从服务器获取图片 则用logo
drawable = getResources().getDrawable(R.drawable.logo);
}
return drawable ;
}
*/
方法二
int WX_THUMB_SIZE = 120;
Bitmap bmp = BackwardSupportUtil.BitmapFactory.getBitmapFromURL(Constants.IMAGE_URL);
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
bmp.recycle();
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
public void handleMessage(Message wxmsg) {
super.handleMessage(wxmsg);
switch (wxmsg.what) {
case 20:
//BitmapDrawable bd = (BitmapDrawable) drawableSharePic;
Log.i("Bitmap", "GET");
//msgthumbData = Util.bmpToByteArray((Bitmap) wxmsg.obj, true);
thumb = (Bitmap) wxmsg.obj;
msg.setThumbImage(thumb); //这里就不行了
//在calss上将 thumb 设为public也不行
//thumbBmp = Bitmap.createScaledBitmap(thumb, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
Log.i("Bitmap", "GET2");
//thumb.recycle();
break;
}
}
mthumb = thumb //如何在外面 使用
msg.setThumbImage(mthumb);
public class DownLoad extends AsyncTask<String, Void, Drawable > {
@Override
protected String doInBackground(String... params) {
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(new URL(params[0]).openStream(), "share_reg.jpg");
} catch (IOException e) {
}
if (drawable == null) {
//如果没有成功从服务器获取图片 则用logo
drawable = getResources().getDrawable(R.drawable.logo);
}
return drawable ;
}
@Override
protected void onPostExecute(Drawable result) {
BitmapDrawable bd = (BitmapDrawable) result;
Bitmap thumb = bd.getBitmap();
msg.setThumbImage(thumb);
// 进行分享
}
}
使用方式new DownLoad().execute(imageurl);使用方式new DownLoad().execute(imageurl);
[quote=引用 3 楼 u014294325 的回复:]
求教要怎么做 最终要能将Drawable 给msg.thumbData 或者setThumbImage 为Bitmap thumb
求教要怎么做 最终要能将Drawable 给msg.thumbData 或者setThumbImage 为Bitmap thumb
网络请求要放到子线程中。
Caused by: android.os.NetworkOnMainThreadException 网络请求不能在主线程操作