Existe um tamanho máximo de bitmap ao usar getDrawingCache?
Estou tentando criar um bitmap do texto em umTextView
. No passado, eu fiz isso usandogetDrawingCache
. No entanto, agora eu preciso criar um bitmap de umTextView
com texto muito mais longo do que antes. Isso está causandogetDrawingCache
para lançar uma NullPointerException.
Embora eu diga "texto muito mais longo", não estou falando de um tempo excessivamente longo. Se eu criar um TextView com 600 pixels de largura na fonte tamanho 24, obtenho a exceção em 53 linhas de texto (mas não em 52 linhas). Existe uma solução alternativa para isso?
No começo eu penseiesta resposta, em que o layout se baseia em umcanvas
, foi a solução. No entanto, isso não funcionou para mim porque estou criando o TextView programaticamente ea largura e a altura são 0 antes de serem definidos. Eu nunca realmente layout meuTextView
na tela.
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;
}
Null Pointer Exception06-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)
...
NotaEste não é um IllegalArgumentException ou OutOfMemoryError (pelo menos não externamente, embora talvez esse seja o motivo internamente).
Stacktrace misterioso no console do desenvolvedor Android (o tamanho do bitmap excede 32 bits)Problema estranho de falta de memória ao carregar uma imagem em um objeto Bitmap