Search code examples
androidandroid-imageview

Post images from the DataBase


I have a DataBase called iiti which has a String called ImgName, the Value of ImgName, which is the name of an Image in the Drawable folder. I want to post that image on an imageView

TextView descText = (TextView) findViewById(R.id.descriptionText);
        descText.setText(iiti.getneighborhood() + "\n");

ImageView iv = (ImageView) findViewById(R.id.imageView);
    int resourceId = this.getResources().getIdentifier(iiti.getLatLat(),"drawable",this.getPackageName());
    iv.setImageResource(resourceId);

the code above well only just make the ImageView a white blank image and if i try to use this code

ImageView iv = (ImageView) findViewById(R.id.imageView);
        iv.setImageResource(R.drawable.hii);
        //hii is a png image in the drawable folder

this is my xml file:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DetailActivity">

    <TextView
        android:id="@+id/cityText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="London"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/neighborhoodText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cityText"
        android:gravity="center"
        android:text="West End"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/cityText"
        android:layout_marginTop="20dp"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitStart"
            android:src="@drawable/london_photo"/>

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1" />

    </LinearLayout>

</RelativeLayout>

the app just crashes this is the Logcat:

03-27 15:21:10.984 9542-9542/com.example.android.mymaps E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.mymaps, PID: 9542
java.lang.RuntimeException: Canvas: trying to draw too large(110250000bytes) bitmap.
at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
at android.graphics.Canvas.drawBitmap(Canvas.java:1420)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)

How can I resolve this error?


Solution

  • Try this in your Activity:

    val resourceId = this.getResources().getIdentifier(iiti.getImgName(), "drawable", this.getPackageName())
    iv.setImageResource(resourceId)
    

    Or this in your Fragment:

    int resourceId = getActivity().getResources().getIdentifier(iiti.getImgName(), "drawable", getActivity().getPackageName());
    iv.setImageResource(resourceId);
    

    In case of any problems check source

    EDIT:

    This is your crash:

    Canvas: trying to draw too large(110250000bytes) bitmap.
    

    So bitmap you are trying to draw is to large... that's it. Compress your file, change it's size or do whatever shrinks it's size. And if you are hasitating - this file is 110.25MB large.