¿Existe un tamaño máximo de mapa de bits cuando se usa getDrawingCache?
Estoy tratando de crear un mapa de bits del texto en unTextView
. En el pasado lo hice usandogetDrawingCache
. Sin embargo, ahora tengo la necesidad de crear un mapa de bits de unTextView
con texto mucho más largo que antes. Esto está causandogetDrawingCache
para lanzar una NullPointerException.
Aunque digo "texto mucho más largo", no estoy hablando de un tiempo irrazonablemente largo. Si creo un TextView que tiene 600 píxeles de ancho con una fuente de tamaño 24, obtengo la excepción en 53 líneas de texto (pero no en 52 líneas). ¿Hay una solución para esto?
Al principio penséesta respuesta, en el que el diseño se dibuja en uncanvas
, fue la solución. Sin embargo, eso no funcionó para mí porque estoy creando TextView mediante programación yel ancho y la altura son 0 antes de que se arreglen. En realidad nunca diseño miTextView
en la pantalla.
private Bitmap getBitmap(Context context){
final int NUMBER_OF_LINES = 53; // 53 crashes, 52 doesn't
final int width = 600; // width of TextView in pixels
final int fontSize = 24;
// create string with NUMBER_OF_LINES
StringBuilder testString = new StringBuilder();
for (int i = 0; i < NUMBER_OF_LINES; i++) {
testString.append("\n");
}
// Create TextView
TextView tvText = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tvText.setTextSize(fontSize);
tvText.setWidth(width);
tvText.setLayoutParams(params);
tvText.setBackgroundColor(Color.WHITE); // even setting the background color affects crashing or not
tvText.setText(testString);
// Create bitmap
tvText.setDrawingCacheEnabled(true);
tvText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tvText.layout(0, 0, tvText.getMeasuredWidth(), tvText.getMeasuredHeight());
tvText.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(tvText.getDrawingCache()); // crashes here
tvText.setDrawingCacheEnabled(false);
// This also didn't work because width and height are 0
/*Bitmap bitmap = Bitmap.createBitmap(tvText.getWidth(), tvText.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tvText.draw(canvas);*/
return bitmap;
}
Excepción de puntero nulo06-21 14:20:24.628 8036-8036/com.example.testsomecode E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testsomecode/com.example.testsomecode.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
...
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:494)
at com.example.testsomecode.MainActivity.getBitmap(MainActivity.java:57) // -> Bitmap bitmap = Bitmap.createBitmap(tvText.getDrawingCache());
at com.example.testsomecode.MainActivity.onCreate(MainActivity.java:25)
...
NotaEsta no es una IllegalArgumentException o OutOfMemoryError (al menos no externamente, aunque quizás esta sea la razón internamente).
Mystery stacktrace en la consola de desarrollador de Android (el tamaño del mapa de bits supera los 32 bits)Extraño problema de falta de memoria al cargar una imagen en un objeto de mapa de bits