简体   繁体   中英

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?

First check image available on device.

File file = new File(pictureLocation);
if (file.exists()) {
    Picasso.with(this).load(new File(pictureLocation)).into(imageView);
} else {
    Log.d("Result", "Image not available");
}

I would suggest you to use Content Provider in this scenario. I had the same issue where the image loaded in the view was black. Storage Access Framework is the solution here

https://developer.android.com/guide/topics/providers/document-provider.html

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);

Because your image load have a large size (ex: 4000px x 3000px). Using Picasso:

int width = new DeviceSize(mContext).widthPixels();
Picasso.get().load(fileUploadPath)
                                    .resize(width, 0)
                                    .onlyScaleDown()
                                    .into(imageView);

--Class DeviceSize--

import android.content.Context;
import android.util.DisplayMetrics;

public class DeviceSize {

    private Context mContext;

    private int widthPixels;
    private int heightPixels;

    public DeviceSize(Context context) {
        mContext = context;
    }

    public int widthPixels() {
        DisplayMetrics lDisplayMetrics = mContext.getResources().getDisplayMetrics();
        widthPixels = lDisplayMetrics.widthPixels;
        return widthPixels;
    }

    public int heightPixels() {
        DisplayMetrics lDisplayMetrics = mContext.getResources().getDisplayMetrics();
        heightPixels = lDisplayMetrics.heightPixels;
        return heightPixels;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM