【问题标题】:ImageView loading issue: works in emulator, doesn't work in a real deviceImageView 加载问题:在模拟器中有效,在真实设备中无效
【发布时间】:2016-08-06 11:12:37
【问题描述】:

我在我的活动中显示了一个简单的ImageView

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

在我的活动课程中,这是我设置图像的方式:

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

问题是,我在模拟器中得到了想要的结果,但在我真正的安卓手机中,什么都没有显示。整个屏幕都是空白的。

由于我已经将图像大小调整为屏幕大小,因此加载高分辨率图像应该没有任何问题。为什么在真机上我的屏幕是空白的?

【问题讨论】:

  • 其他数据是否显示
  • 我的布局中只有一个 ImageView,没有其他数据。
  • 如何获取本地或服务器图像?
  • 本地。它写在我的代码中。 .load(new File(pictureLocation))
  • 您的外部或内部存储中有图像???

标签: android bitmap imageview picasso


【解决方案1】:

首先检查设备上可用的图像。

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

【讨论】:

  • 感谢您的回答,但该文件已存在于我的设备中,我也有权读取它。问题是,如果代码可以在 Android M 模拟器中运行,为什么它不能在真实设备中运行。
  • 给图片位置数据
  • 正如日志所说,/storage/emulated/0/Pictures/MyCameraApp/IMG_20160806_170840.jpg 是位置。我浏览了我的手机,手动验证它在那里。然后我验证了日志是否存在。
  • 请移除调整大小并检查。
  • 删除调整大小再次无效。
【解决方案2】:

我建议您在这种情况下使用 Content Provider。 我遇到了同样的问题,视图中加载的图像是黑色的。存储访问框架是这里的解决方案

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

【讨论】:

    【解决方案3】:

    经过一番研究,这是原因和解决方案:

    由于ImageView 无法加载大尺寸图像(相机为 13MP,图像为 3-4 MB),因此在真实设备中屏幕会变为空白。我尝试了一个较小的图像(~100 KB),效果很好。可惜毕加索和格莱德都做不到。

    因此,我首先调整了图像的大小,然后将它们压缩到 100 KB 范围内(如果您想要全高清图像,则需要另一种方法):

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

    【讨论】:

      【解决方案4】:

      因为您加载的图片尺寸很大(例如:4000 像素 x 3000 像素)。 使用毕加索:

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

      --类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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        • 1970-01-01
        • 2012-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多