【问题标题】:Image shows on emulator but not on real device图像显示在模拟器上,但不在真实设备上
【发布时间】:2016-04-27 10:06:55
【问题描述】:

我正在开发一个 Android 项目,在该项目中我使用了一个自定义 ListView,左侧有一个图像。
我使用 Genymotion 模拟器,效果很好。图像完美显示: Screenshot from Genymotion emulator

但是,当我在我的设备(Huawei P8 Lite)上使用该应用程序时,什么都没有显示:Screenshot from device

我试图在我的设备上调试应用程序,图像路径存在(否则它们将是“无可用图像”)答案是:/storage/emulated/0/Pictures/JPEG_20160415_134349_- 350854394.jpg
我去了 Android 设备监视器,图像存在,但路径略有不同(mnt/shell/ 而不是 /storage/,但我认为这不是真正的问题,是吗?)
我在堆栈跟踪中没有任何“OutOfMemory”错误或任何其他错误。
这是我用来显示图片的代码:

    if(values.get(position).getImgList().size()==0){
        imageView.setImageResource(R.drawable.nopics);
    }
    else {
    //getImg() return the path (string) of the first image in a ArrayList of path
        imageView.setImageBitmap(values.get(position).getImg());
    }

我很确定 getImg() 会返回一个图像,因为我在将图像发送到 Web 服务时使用了相同的方法。网络服务正确接收图像。
我的 Android 清单中还有读/写内部/外部存储。

编辑:我忘记了布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<ImageView
    android:id="@+id/icon"
    android:layout_width="200px"
    android:layout_height="200px"
    android:layout_marginLeft="40px"
    android:layout_marginRight="10px"
    android:layout_marginTop="4px">
</ImageView>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<TextView
    android:id="@+id/label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="70px"
    android:layout_marginLeft="10px"
    android:text="@+id/label"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:textSize="48px" >
</TextView>

<ImageButton
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/croixrouge"
    android:src="@drawable/croixrouge"
    android:layout_alignBottom="@+id/label"
    android:layout_marginBottom="50px"
    android:layout_alignEnd="@+id/label" />

    <ImageButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/option"
        android:src="@drawable/modif"
        android:layout_alignTop="@+id/croixrouge"
        android:layout_toStartOf="@+id/croixrouge"
        android:layout_marginRight="5px"/>

    <ImageButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/refresh"
        android:src="@drawable/refreshpurple"
        android:layout_alignTop="@+id/option"
        android:layout_toStartOf="@+id/option"
        android:layout_marginRight="5px" />
</RelativeLayout>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10px"
    android:id="@+id/sublabel"
    android:text="@+id/sublabel"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:textSize="32px"/>

</LinearLayout>

</LinearLayout>  

getImg() 代码:

public Bitmap getImg() {
    if(imgList.size() !=0) {
        Bitmap bmp = BitmapFactory.decodeFile(imgList.get(0));
        return bmp;
    }
    return null;
}

编辑:我如何选择图像: 受保护的无效onActivityResult(int requestCode,int resultCode, 意图图像数据){ super.onActivityResult(requestCode, resultCode, imageData);

    switch (requestCode) {
        case Define.ALBUM_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                path = imageData.getStringArrayListExtra(Define.INTENT_PATH);
...
            }
    }
}  

后来:

 r.setImgList(path);  

有人可以帮我理解为什么图像没有显示在真实设备上吗?

【问题讨论】:

  • 它不适合在 imageview 中试试这个 android:scaleType="fitXY"
  • 模拟器和设备的api等级是多少?
  • scaleType 的东西不起作用,不过谢谢。模拟器和设备都是 API 21,设备在 android 5.0.1 上运行,模拟器在 5.0.0 上运行
  • getImg() 返回什么?
  • @KNeerajLal 图片的路径,正如我在帖子中所写的那样,/storage/emulated/0/Pictures/JPEG_20160415_134349_-350854394.jpg

标签: java android image emulation device


【解决方案1】:

当您尝试在 ImageView 上加载更高分辨率的图像时会发生这种情况。您必须将其缩小为多种尺寸(ldpi、mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi)以支持多种屏幕分辨率和尺寸。

请参考这个link

【讨论】:

    【解决方案2】:

    您可以使用此辅助方法来获取图像的真实路径。在您的 getImg() 方法中传递从辅助方法创建的路径:

    public Bitmap getImg() {
        if(imgList.size() != 0) {
            Uri uri = Uri.parse(imgList.get(0));
            String path = getRealPathFromUri(getActivity(), uri)
            Bitmap bmp = BitmapFactory.decodeFile(path);
            return bmp;
        }
        return null;
    }
    
    public String getRealPathFromURI(Context context, Uri uri) {
        String path;
        if ("content".equals(uri.getScheme())) {
            path = getRealPathFromContentURI(context, uri);
        } else if ("file".equals(uri.getScheme())) {
            path = uri.getPath();
        } else {
            path = uri.toString();
        }
        return path;
    }
    
    private String getRealPathFromContentURI(Context context, Uri contentUri) {
        if (contentUri == null) {
            return null;
        }
    
        String[] proj = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
    
        if (cursor == null) {
            return null;
        }
    
        int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        if (column_index == -1) {
            cursor.close();
            return null;
        }
    
        String path;
        if (cursor.moveToFirst()) {
            path = cursor.getString(column_index);
        } else {
            path = null;
        }
    
        cursor.close();
        return path;
    }
    

    【讨论】:

    • 感谢您的回答。然而,这并没有解决问题。在模拟器上它仍然有效(但它调整了我的图片,不知道为什么)但在我的 Android 设备上仍然是同样的问题
    • 如何获取将保存到 values 数组中的图片路径?
    • 对于您的布局代码,始终使用 dp 值而不是 px 以使大小在各种 android 设备中保持一致。
    【解决方案3】:

    解决了问题!
    我缩放了位图。好像放不下屏幕什么的……

    public Bitmap getImg() {
        if(imgList.size() !=0) {
            Bitmap bmp = BitmapFactory.decodeFile(imgList.get(0));
            int nh = (int) ( bmp.getHeight() * (800.0 / bmp.getWidth()) );
            Bitmap scaled = Bitmap.createScaledBitmap(bmp, 600, nh, true);
            return scaled;
        }
        return null;
    } 
    

    感谢您的帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多