0

I am trying to generate images from a string array, by converting the string text to bitmap through paint canvas, and i am generating 50 images in a single activity.

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    Bitmap copy;
    DisplayMetrics displayMetrics = new DisplayMetrics ();
    getWindowManager ().getDefaultDisplay ().getMetrics (displayMetrics);
    int width = displayMetrics.widthPixels;

    String text = listArray[position];
    text.replace("\'", "");
    text.replace("\"", "");

    Bitmap icon = BitmapFactory.decodeResource (mContext.getResources (), R.drawable.abraham);
    TextPaint paint = new TextPaint ();
    paint.setColor (Color.parseColor ("#000000"));
    paint.setTextSize ((width / 10) - (text.length () / 6));
    paint.setTypeface (Typeface.create (Typeface.createFromAsset (mContext.getAssets (), "fonts/underwood_champion.ttf"), Typeface.BOLD));
    copy = icon.copy (Bitmap.Config.RGB_565, true);
    Canvas canvas = new Canvas (copy);
    StaticLayout layout = new StaticLayout (text, paint, canvas.getWidth () - 100, Layout.Alignment.ALIGN_CENTER, 1.3f, 3, false);
    canvas.translate (100, (width / 2)+30);//(canvas.getHeight()/2)-(text.length ())); //position the text
    layout.draw (canvas);

    int size = width / 2;
    imageView = new ImageView (mContext);
    imageView.setLayoutParams (new GridView.LayoutParams (size, size));
    imageView.setScaleType (ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding (30, 30, 30, 30);
    imageView.setImageBitmap (copy);
    return imageView;
}

Note: I have already did the step.

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

This is the logcat

    PID: 7957 java.lang.OutOfMemoryError: Failed to allocate a 7144212 byte allocation with 4194304 free bytes and 4MB until OOM at 
dalvik.system.VMRuntime.newNonMovableArray(Native Method) at
android.graphics.Bitmap.nativeCopy(Native Method) at 
android.graphics.Bitmap.copy(Bitmap.java:592) at 
com.ahsaashafeez.textoverbitmap.MainActivity$ImageAdapter.getView(MainActivity.java:84) at
android.widget.AbsListView.obtainView(AbsListView.java:2362) at 
android.widget.GridView.makeAndAddView(GridView.java:1439) 
Ishan Fernando
  • 2,758
  • 1
  • 31
  • 38
  • What is the issue?. Please describe the issue and add logcat output. – Ishan Fernando Feb 02 '19 at 12:23
  • Process: com.ahsaashafeez.textoverbitmap, PID: 7957 java.lang.OutOfMemoryError: Failed to allocate a 7144212 byte allocation with 4194304 free bytes and 4MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.Bitmap.nativeCopy(Native Method) at android.graphics.Bitmap.copy(Bitmap.java:592) at com.ahsaashafeez.textoverbitmap.MainActivity$ImageAdapter.getView(MainActivity.java:84) at android.widget.AbsListView.obtainView(AbsListView.java:2362) at android.widget.GridView.makeAndAddView(GridView.java:1439) – Hafeez Ul Haq Feb 02 '19 at 12:31
  • It vorks fine over my emulator as vvell as the higher versions, but some times in the lovver versions it causes issues – Hafeez Ul Haq Feb 02 '19 at 12:33
  • Try to put your image files into correct res directory e.g. drawable-hdpi, drawable-xhdpi etc. If you are using single size images for all resolutions put them in drawable-nodpi. – Ranjan Feb 02 '19 at 12:36
  • no-dpi vvorked for me brother! thanks for your help! – Hafeez Ul Haq Feb 02 '19 at 15:52

0 Answers0