Image is not set in imageview onelplus , lollipop

482 Views Asked by At

I was trying to create a tutorial like in zomato app, its working in all phones I tested, other than oneplus devices, in oneplus one and oneplus X, the image is not set and imageview is transparent. I tried both programmatically and through xml to set the image. Is there any problem with using high res image in these devices with the android version lollipop.

My activity:

int ht,wi;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tutorial);

            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;
 wi = width *2/3;
        ht  = height * 3/5;

            ImageView wireframe = (ImageView) findViewById(R.id.wireframe);
            BitmapWorkerTask task = new BitmapWorkerTask(wireframe);
            task.execute(R.drawable.wireframe);


    }

    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;
    }
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {

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

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

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }


    class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        private int data = 0;

        public BitmapWorkerTask(ImageView imageView) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        // Decode image in background.
        @Override
        protected Bitmap doInBackground(Integer... params) {
            data = params[0];
            Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), data, wi, ht);
            return  bitmap;
        }

        // Once complete, see if ImageView is still around and set bitmap.
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }

My xml file:

 <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2">
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:descendantFocusability="afterDescendants">
                      <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_marginTop="30dp">
                        <ImageView
                            android:id="@+id/wireframe"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:src="@drawable/wireframe"/>
                    </RelativeLayout>
                    <TextView
                        android:id="@+id/tutorial_skip"
                        android:onClick="onLogin"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="SKIP LOGIN"
                        android:padding="5dp"
                        android:layout_gravity="right"
                        android:layout_marginRight="16dp"
                        android:layout_marginTop="10dp"
                        android:textColor="#ffffff"
                        android:textSize="15dp"
                        android:textStyle="bold"/>
                </FrameLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_weight="3"
                android:id="@+id/bottom_layout"
                android:gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#ffffff">
             </LinearLayout>
         </LinearLayout>

The image I tried to load:

the Image I tried to load

1

There are 1 best solutions below

0
On BEST ANSWER

I used newimage with much less resolution and now in the imageview is showing the image

new image i used