Salah satu penerapan aplikasi adalah menampilkan image. Untuk menampilkan image dengan resolusi kecil atau sedikit bukan masalah, karena memory yang digunakan kecil. Namun akan menjadi masalah ketika image yang ditampilkan memiliki resolusi yang sangat besar, maka Anda kemungkinan akan mengalami berupa error “Out Of Memory”.

Untuk menangani ini bisa ditangani dengan menurunkan kepadatan pixel, atau insamplesize, namun hasil imagenya menjadi sangat tidak bagus, menjadi blurry / kabur.

Setelah melakukan searching… ini mungkin salah satu solusinya yang berhasil saya gunakan.

Ini dibagian saat akan menampilkan image :

FileInputStream inputStream = new FileInputStream(file);
Bitmap mBitmap = decodeSampledBitmapFromResourceMemOpt(inputStream, 800,
                    800);
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(mBitmap);

Ini fungsi dari decodeSampledBitmapFromResourceMemOpt :

public Bitmap decodeSampledBitmapFromResourceMemOpt(
            InputStream inputStream, int reqWidth, int reqHeight) {

        byte[] byteArr = new byte[0];
        byte[] buffer = new byte[1024];
        int len;
        int count = 0;

        try {
            while ((len = inputStream.read(buffer)) > -1) {
                if (len != 0) {
                    if (count + len > byteArr.length) {
                        byte[] newbuf = new byte[(count + len) * 2];
                        System.arraycopy(byteArr, 0, newbuf, 0, count);
                        byteArr = newbuf;
                    }

                    System.arraycopy(buffer, 0, byteArr, count, len);
                    count += len;
                }
            }

            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(byteArr, 0, count, options);

            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
            options.inPurgeable = true;
            options.inInputShareable = true;
            options.inJustDecodeBounds = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            int[] pids = { android.os.Process.myPid() };
            MemoryInfo myMemInfo = mAM.getProcessMemoryInfo(pids)[0];
            Log.e(TAG, "dalvikPss (decoding) = " + myMemInfo.dalvikPss);

            return BitmapFactory.decodeByteArray(byteArr, 0, count, options);

        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
    }

Dan ini fungsi calculateInSampleSize :

private static int calculateInSampleSize(
    BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
     }

     return inSampleSize;
   } 

Done, silahkan sesuaikan dengan codingan Anda. Saya mendapatkan referensi ini di sini dan di sini

Tulisan Lain   Mengambil Screenshot Android Tanpa Root(ing)

credit untuk mereka

By alfach

Leave a Reply

Your email address will not be published. Required fields are marked *