Search code examples
androidimageviewfullscreenmode

High resolution image not displaying in image view when view in full screen mode?


I am displaying an image in full screen mode in samsung tab2,but whenever going to display large image then nothing display in the image view.

image_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_repet_"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgFullImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_margin="15dp"
      />

</LinearLayout>

FullImageActivity.java:-

    ImageView imgFullImage;

    String dispFilename;
    String fileUrl;
    Bitmap bitmap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.image_view);

        imgFullImage = (ImageView)findViewById(R.id.imgFullImage);

        fileUrl = getIntent().getExtras().getString("fileurl");
        dispFilename=getIntent().getExtras().getString("dispFilename");

            imgFullImage.setScaleType(ScaleType.FIT_CENTER);

                bitmap=decodeSampledBitmapFromResource(fileUrl,
        getDeviceWidth(FullImageActivity.this),getDeviceHeight(FullImageActivity.this));

               Log.e("Fill Image Size in 
                 Bytes","====>"+bitmap.getByteCount());     
        Log.e("Bitmap width & height","===>"+bitmap.getWidth() +"x"+bitmap.getHeight());
                imgFullImage.setImageBitmap(bitmap);


    }

  public static int getDeviceWidth(Activity activity) {
        int deviceWidth = 0;

        Point size = new Point();
        WindowManager windowManager = activity.getWindowManager();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(size);
            deviceWidth = size.x;
        } else {
            Display display = windowManager.getDefaultDisplay();
            deviceWidth = display.getWidth();
        }
        return deviceWidth;
    }

@SuppressLint("NewApi")
public static int getDeviceHeight(Activity activity) {
    int deviceHeight = 0;

    Point size = new Point();
    WindowManager windowManager = activity.getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        windowManager.getDefaultDisplay().getSize(size);
        deviceHeight = size.y;
    } else {
        Display display = windowManager.getDefaultDisplay();
        deviceHeight = display.getHeight();
    }
    return deviceHeight;
}

public static Bitmap decodeSampledBitmapFromResource(String pathName,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pathName, options);
}

public 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) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and
        // keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

Log:-

12-13 13:31:45.238: E/Fill Image Size in Bytes(24036): ====>15668224
12-13 13:31:45.238: E/Bitmap width & height(24036): ===>2288x1712

if any one have idea please reply.

Thanks in advance...


Solution

  • public static Bitmap decodeSampledBitmapFromResource(String pathName,
                int reqWidth, int reqHeight) {
    
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(pathName, options);
    
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(pathName, options);
        }
    
        public 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) {
    
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
    
                // Calculate the largest inSampleSize value that is a power of 2 and
                // keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }
    
            return inSampleSize;
        }