您好,登錄后才能下訂單哦!
BitmapFactory.decodeStream引起了OutOfMemory. 在decodeStream之前,通過設置options.inJustDecodeBounds = true;讓Bitmap實現虛加載。
設置options.inJustDecodeBounds = true;讓Bitmap實現虛加載。 使用options.outWidth和options.outHeight獲取圖片寬和高。 再使用ImageLoader.getInstance().displayImage加載圖片。
public class PhotoViewFragment extends Fragment { private PhotoView mGestureImageView; private Bitmap mBitmap; @Override public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { View inflateView = inflater.inflate(R.layout.fragment_photo_view_show, container, false); mGestureImageView = (PhotoView) inflateView.findViewById(R.id.p_w_picpath_turn_show_gestureImageView); getBundleData(); try { /*減少內存占用*/ BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; opt.inPurgeable = true; opt.inInputShareable = true; opt.inJustDecodeBounds = true;//虛加載 FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory() + File.separator + "abc.png"); mBitmap = BitmapFactory.decodeStream(fis, null, opt); int width = opt.outWidth; int height = opt.outHeight; if (height / width >= 1.6) {//判斷橫豎圖 mGestureImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); } else { mGestureImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); } DisplayImageOptions op = new DisplayImageOptions.Builder() .cacheInMemory(true) // default .build(); Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + File.separator + "abc.png")); ImageLoader.getInstance().displayImage(uri.toString(), mGestureImageView, op); } catch (Exception e) { Log.e("Fragment", "onCreateView p_w_picpath-view", e); } return inflateView; } /** * 重置GestureImageView為原大小 */ public void resetGestureImageView() { mGestureImageView.reset(); } /** * 獲得Bundle */ private void getBundleData() { Bundle bundle = getArguments(); } @Override public void onPause() { super.onPause(); mGestureImageView.reset(); } @Override public void onDestroy() { super.onDestroy(); if (mBitmap != null && !mBitmap.isRecycled()) { mBitmap.recycle(); mBitmap = null; } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。