Search code examples
androidbitmapimageviewpicasso

ImageView loading issue: works in emulator, doesn't work in a real device


I'm showing a simple ImageView in my activity:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.package.name.EditPicture">

    <ImageView
        android:id="@+id/problem_picture"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</RelativeLayout>

In my activity class, this is how I'm setting the image:

//first calculate the width and height of the screen
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

//Then, resize the image's width and height equal to that of the screen:
Picasso.with(this).load(new File(pictureLocation)).rotate(90f).resize(height,width).into(imageView);

The problem is, I'm getting the desired result in the emulator, but in my real android phone, nothing is shown. Whole screen is blank.

As I'm already resizing down the image to that of the screen-size, there shouldn't be any issues on loading a high-resolution image. Why is my screen blank then in the real device?


Solution

  • After a little research, here's the reason and a solution:

    The screen is going blank in a real device because the ImageView is unable to load a big-ass image (with camera being 13MP, the images were 3-4 MB). I tried a smaller image (~100 KB) and that worked pretty well. It's sad that neither Picasso nor Glide could do the thing.

    Therefore, I first resized the images and then compressed them to fall in the 100 KB range (You need a different approach if you want the full HD image):

    /**
     * getting the screen height and width, so that we could resize the image accordingly
     */
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
    
    
    /**
     * Getting the old photo and then resizing it to the size of the screen.
     * We are also compressing it. 70 is a number between 0 to 100.
     * You see, close to 0 means very low quality but very small in size image
     * Close to 100 means very high quality, but the size will be big.
     */
     Bitmap photo = BitmapFactory.decodeFile(pictureLocation);
     photo = Bitmap.createScaledBitmap(photo, width, height, false);
     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     photo.compress(Bitmap.CompressFormat.JPEG, 70, bytes);
    
    
     /**
      * fetching the location where this has to be saved. folder location is the location of my Pictures folder.
      */
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
      String smallFileLocation = folderLocation + File.separator + "IMG_" + timeStamp + ".jpg";
    
      /**
       * New file is saved at this place now.
       */
      File f = new File(smallFileLocation);
      f.createNewFile();
      FileOutputStream fo = new FileOutputStream(f);
      fo.write(bytes.toByteArray());
      fo.close();
    
    
      /**
       * Later, we can simply put the picture in our ImageView using Picasso or just imageView.setImageBitmap
       */
      Picasso.with(this).load(new File(smallFileLocation)).rotate(90f).resize(height,width).into(imageView);