বাংলায় অ্যান্ড্রয়েড সহায়িকা - Bangla Android Guide

বড় বিট ম্যাপ ব্যবস্থাপনা ডিজাইন করা

অ্যান্ড্রয়েড সিস্টেমের একটি সীমিত আকারের মেমরী আছে, সুতরাং উচ্চ-রেজ্যুলেশনের ইমেজ ডাউনলোড করা এবং স্টোর করা আপনার অ্যাপে আউট-অফ-মেমরী এরর এর কারন হতে পারে। এটা পরিহার করতে, নিচের টিপসগুলো অনুসরণ করুন:

  • ইমেজ লোড করুন শুধুমাত্র তখনই যখন তারা স্ক্রিনে প্রদর্শিত হয়। উদাহরণস্বরূপ, যখন একটি GridView বা Gallery তে মাল্টিপল ইমেজ প্রদর্শন করা হয়, শুধুমাত্র একটি ইমেজ লোড করুন যখন getView() কে ভিউয়ের Adapter এ কল করা হয়।

  • Bitmap ভিউয়ে recycle()কল করুন যার আর কোন প্রঢোজন নেই।

  • একটি ইন-মেমরী collection এর মধ্যে Bitmap অবজেক্টে রেফারেন্স স্টোর করার জন্য WeakReference ব্যবহার করুন।

  • আপনি ডদি নেটওয়ার্ক থেকে ইমেজ পেয়ে (ফেচ করে থাকেন) থাকেন, দ্রুত প্রবেশের জন্য HD কার্ডে তাদের ফেচ করতে বা স্টোর করতে AsyncTask ব্যবহার করুন। অ্যাপলিকেশনের ইউআই থ্রেডে কখনই নেটওয়ার্ক ট্রানজেকশন (লেনদেন) করবেন না।

  • সত্যিকারের বড় ইমেজকে সঠিক সাইজে আনতে পিক্সেল কমিয়ে আনুন যেহেতু আপনি এটাকে ডাউনলোড করেছেন, অন্যথায়, ডাউনলোড করা ইমেজ নিজেই "Out of Memory"এর কারন হয়ে দাড়াবে। এখানে কিছু নমুনা কোড দেয় হলো যা ইমেজ কে স্কেলডাউন করে যখন ডাউনলোড করা হয়:

  // Get the source image's dimensions
  BitmapFactory.Options options = new BitmapFactory.Options();
  // This does not download the actual image, just downloads headers.
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(IMAGE_FILE_URL, options);
  // The actual width of the image.
  int srcWidth = options.outWidth;
  // The actual height of the image.
  int srcHeight = options.outHeight;

  // Only scale if the source is bigger than the width of the destination view.
  if(desiredWidth > srcWidth)
    desiredWidth = srcWidth;

  // Calculate the correct inSampleSize/scale value. This helps reduce memory use. It should be a power of 2.
  int inSampleSize = 1;
  while(srcWidth / 2 > desiredWidth){
    srcWidth /= 2;
    srcHeight /= 2;
    inSampleSize *= 2;
  }

  float desiredScale = (float) desiredWidth / srcWidth;

  // Decode with inSampleSize
  options.inJustDecodeBounds = false;
  options.inDither = false;
  options.inSampleSize = inSampleSize;
  options.inScaled = false;
  // Ensures the image stays as a 32-bit ARGB_8888 image.
  // This preserves image quality.
  options.inPreferredConfig = Bitmap.Config.ARGB_8888;

  Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(IMAGE_FILE_URL, options);

  // Resize
  Matrix matrix = new Matrix();
  matrix.postScale(desiredScale, desiredScale);
  Bitmap scaledBitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0,
      sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);
  sampledSrcBitmap = null;

  // Save
  FileOutputStream out = new FileOutputStream(LOCAL_PATH_TO_STORE_IMAGE);
  scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  scaledBitmap = null;