@Override public void run() { if (path == null || path.length() == 0) { Log.e("loadBitmap", "error : path is empty"); } if (imageView == null) { Log.e("loadBitmap", "error : imageView is null"); } if (callback == null) { Log.e("loadBitmap", "error : callback is null"); } bitmap = ImageUtil.getBitmapFromFileLimitSize(path, max); int degree = BitmapUtil.getInstance().readPictureDegree(path); if (degree != 0) { bitmap = BitmapUtil.getInstance().rotateBitmap(degree, bitmap); } String key = path + "_" + type.ordinal(); if (type == ExplorerPhotoType.FILELIST) { bitmap = BitmapUtil.getInstance().cropBitmapFix(bitmap, max / 2, max / 2); } else if (type == ExplorerPhotoType.GRID) { bitmap = BitmapUtil.getInstance().cropBitmapFix(bitmap, max, max); } else if (type == ExplorerPhotoType.GALLERY) { } if (bitmap != null) { BitmapMemoryLruCache.getInstance().addBitmapToMemoryCache(key, bitmap); } new Handler(Looper.getMainLooper()) .post( new Runnable() { // imageView.post(new Runnable() { // This method can be invoked from outside of the UI thread only when this View is // attached to a window. // View.post 可能导致不执行,内存泄露等问题 // imageView.getHandler().post(new Runnable() { @Override public void run() { if (imageView.getContentDescription().toString().equalsIgnoreCase(path)) { callback.dealBitmap(imageView, bitmap); } path = null; imageView = null; callback = null; bitmap = null; max = 0; } }); }
/** * ** display a image from the network * * @param url image url * @param view imageView * @return null */ public void DisplayImage(String url, ImageView view) { imageViews.put(view, url); Bitmap bitmap = null; // memoryCache.get(url); File f = fileCache.getFile(url); if (f != null) bitmap = BitmapUtil.decodeFile(f, 480); if (bitmap != null) { view.setImageBitmap(BitmapUtil.handleReflection(bitmap)); } else { // Log.e("no cache in memory" , "downloading!"); queuePhoto(url, view); Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder); view.setImageBitmap(BitmapUtil.handleReflection(bitmap1)); } }
// 使用快捷分享完成图文分享 public static void showGrid(Context mContext, Good mGood) { Intent i = new Intent(mContext, ShareAllGird.class); // 分享时Notification的图标 i.putExtra("notif_icon", com.liangcang.R.drawable.ic_launcher); // 分享时Notification的标题 i.putExtra("notif_title", mContext.getString(R.string.app_name)); // // address是接收人地址,仅在信息和邮件使用,否则可以不提供 // i.putExtra("address", "12345678901"); // title标题,在印象笔记、邮箱、信息、微信(包括好友和朋友圈)、人人网和QQ空间使用,否则可以不提供 i.putExtra("title", "分享,正式发布没有该页面,直接发送"); // titleUrl是标题的网络链接,仅在人人网和QQ空间使用,否则可以不提供 i.putExtra("titleUrl", "http://sharesdk.cn"); // text是分享文本,所有平台都需要这个字段 i.putExtra("text", "良仓商品 " + mGood.getGoods_name() + " 不错哦 ,刚看来看看吧"); // imagePath是本地的图片路径,所有平台都支持这个字段,不提供,则表示不分享图片 i.putExtra("imagePath", BitmapUtil.getFileName(mGood.getGoods_image())); // url仅在微信(包括好友和朋友圈)中使用,否则可以不提供 i.putExtra("url", "http://www.iliangcang.com"); // thumbPath是缩略图的本地路径,仅在微信(包括好友和朋友圈)中使用,否则可以不提供 // i.putExtra("thumbPath", MainActivity.TEST_IMAGE); // appPath是待分享应用程序的本地路劲,仅在微信(包括好友和朋友圈)中使用,否则可以不提供 // i.putExtra("appPath", MainActivity.TEST_IMAGE); // comment是我对这条分享的评论,仅在人人网和QQ空间使用,否则可以不提供 // i.putExtra("comment", menu.getContext().getString(R.string.share)); // site是分享此内容的网站名称,仅在QQ空间使用,否则可以不提供 // i.putExtra("site", menu.getContext().getString(R.string.app_name)); // siteUrl是分享此内容的网站地址,仅在QQ空间使用,否则可以不提供 // i.putExtra("siteUrl", "http://sharesdk.cn"); // 是否直接分享 i.putExtra("silent", false); mContext.startActivity(i); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress); String url = MainActivity.URL; Uri uri = Uri.parse(url); final ImageView draweeView = (ImageView) findViewById(R.id.image); draweeView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), MainActivity2.class); startActivity(intent); } }); BitmapUtil.loadPlainBitmap( uri, new OnResourceReadyCallback() { @Override public void onReady(final Drawable drawable) { runOnUiThread( new Runnable() { @Override public void run() { progressBar.setVisibility(View.GONE); draweeView.setImageDrawable(drawable); if (drawable instanceof AnimatableDrawable) { ((AnimatableDrawable) drawable).start(); } } }); } @Override public void onFail(Throwable cause) {} @Override public void onProgressUpdate(final float progress) { runOnUiThread( new Runnable() { @Override public void run() { progressBar.setProgress((int) progress); } }); } }); }
@Override public void run() { if (imageViewReused(photoToLoad)) return; Bitmap bmp = getBitmap(photoToLoad.url); // download bmp = BitmapUtil.handleReflection(bmp); memoryCache.put(photoToLoad.url, bmp); if (imageViewReused(photoToLoad)) return; BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad); Activity a = (Activity) photoToLoad.imageView.getContext(); a.runOnUiThread(bd); }
/** * return a bitmap from a url * * @param url image url * @return bitmap */ public Bitmap getBitmap(String url) { File f = fileCache.getFile(url); // from SD cache Bitmap b = BitmapUtil.decodeFile(f, 480); if (b != null) return b; // from web try { Bitmap bitmap = null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); OutputStream os = new FileOutputStream(f); BitmapUtil.CopyStream(is, os); os.close(); bitmap = BitmapUtil.decodeFile(f, 480); return bitmap; } catch (Exception ex) { ex.printStackTrace(); return null; } }
/** * Decode image in background. * * @param params ignored * @return the decoded bitmap data */ @Override protected Result doInBackground(Void... params) { try { if (!isCancelled()) { BitmapUtil.DecodeBitmapResult decodeResult = BitmapUtil.decodeSampledBitmap(mContext, mUri, mWidth, mHeight); if (!isCancelled()) { BitmapUtil.RotateBitmapResult rotateResult = mPreSetRotation != null ? BitmapUtil.rotateBitmapResult(decodeResult.bitmap, mPreSetRotation) : BitmapUtil.rotateBitmapByExif(mContext, decodeResult.bitmap, mUri); return new Result( mUri, rotateResult.bitmap, decodeResult.sampleSize, rotateResult.degrees); } } return null; } catch (Exception e) { return new Result(mUri, e); } }
/** 将下载好的图片存放到文件中 */ private void setBitmapToFile(String imageName, InputStream is) { File file = null; FileOutputStream fos = null; try { file = new File(cacheDirPath_, imageName); if (!file.exists()) { File file2 = new File(cacheDirPath_); file2.mkdirs(); } if (Utils.hasSDCard()) { fos = new FileOutputStream(file); } else { fos = context.openFileOutput(cacheDirName + "/" + imageName, Context.MODE_PRIVATE); } int len = -1; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); fos.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 100, 100); if (null != bitmap) { Bitmap compressBitmap = BitmapUtil.compressImage(bitmap); String cachePath = Utils.getCacheDir(context, "picture"); setBitmapToFile(cachePath, imageName, compressBitmap); } } }
@Override protected Bitmap doInBackground(String... params) { imageUrl = params[0]; FileDescriptor fileDescriptor = null; FileInputStream fileInputStream = null; // 读取硬盘缓存对象 Snapshot snapshot = null; try { // 生成图片Url对应的key final String key = imageLoader.hashKeyForDisk(imageUrl); // 查找key对应的缓存 snapshot = imageLoader.diskCache.get(key); if (snapshot == null) { LruDiskCache.Editor editor = imageLoader.diskCache.edit(key); if (editor != null) { OutputStream outputStream = editor.newOutputStream(0); // 网络获取Bitmap写入指定输出流 if (downloadUrlToString(imageUrl, outputStream)) { // 提交生效 editor.commit(); } else { // 放弃此次写入 editor.abort(); } } // 写入缓存后,再次查找对应的缓存 snapshot = imageLoader.diskCache.get(key); } if (snapshot != null) { // 读取缓存文件 fileInputStream = (FileInputStream) snapshot.getInputStream(0); fileDescriptor = fileInputStream.getFD(); } } catch (IOException e) { e.printStackTrace(); } Bitmap bitmap = null; if (fileDescriptor != null) { bitmap = BitmapUtil.decodeSampleBitmap(fileDescriptor, imageSize.width, imageSize.height); } if (bitmap != null) { // 图片下载完缓存到lrucache中 imageLoader.addBitmapToMemoryCache(params[0], bitmap); } return bitmap; }
public BitmapWorkerTask(ImageLoader imageLoader, ImageView imageView) { this.imageLoader = imageLoader; this.imageView = imageView; imageSize = BitmapUtil.getImageViewSize(imageView); }