Reputation: 171
I'm working on an Android project where I use a custom ListView with an image on the left.
I use Genymotion emulator, which works fine. The image is shown perfectly:
Screenshot from Genymotion emulator
However, when I use the app on my device (Huawei P8 Lite), nothing is shown: Screenshot from device
I tried to debug the app on my device, the image path exists (otherwise their would be the "No image available" instead) ans is: /storage/emulated/0/Pictures/JPEG_20160415_134349_-350854394.jpg
I went to the Android device monitor and the image exists, but at a slightly different path (mnt/shell/ instead of /storage/, but I don't think this is the real issue, is it ?)
I don't have any "OutOfMemory" error or any other error in the stacktrace.
This is the code I use to show the picture:
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());
}
I'm pretty sure that getImg() return an image as I use the same method when I send the Image to a webservice. The webservice receive the image correctly.
I also have the read/write internal/external storage in my Android Manifest.
EDIT: I forgot the layout code:
<?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() code:
public Bitmap getImg() {
if(imgList.size() !=0) {
Bitmap bmp = BitmapFactory.decodeFile(imgList.get(0));
return bmp;
}
return null;
}
EDIT: How I pick the image: protected void onActivityResult(int requestCode, int resultCode, Intent imageData) { super.onActivityResult(requestCode, resultCode, imageData);
switch (requestCode) {
case Define.ALBUM_REQUEST_CODE:
if (resultCode == RESULT_OK) {
path = imageData.getStringArrayListExtra(Define.INTENT_PATH);
...
}
}
}
And later:
r.setImgList(path);
Could someone please help me understand why the image is not displayed on a real device ?
Upvotes: 0
Views: 2235
Reputation: 171
Fixed the problem !
I scaled the bitmap. It seems like it couldn't fit the screen or something ...
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;
}
Thank you for your help
Upvotes: 0
Reputation: 954
You can use this helper methods to get the real path of your image. In your getImg() method pass the path created from the helper method:
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;
}
Upvotes: 0
Reputation: 11032
This happens when you try to load the image with higher resolutions on ImageView. You have to scale it down to multiple sizes (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) to support multiple screen resolutions and dimensions.
Please refer to this link
Upvotes: 0