我已经以编程方式创建了一个tablelayout,其中包含从服务器获取的数据,并且有一个操作栏按钮,用于将整个tablelayout共享为已安装应用程序上的图像。但它崩溃了应用程序与OOM异常。 TableLayout仅包含具有一些可绘制外观和外观布局的文本以及大约800行
Activity.xml
<ScrollView
android:id="@+id/scrollView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/webview"
android:scrollbars="none"
android:layout_alignParentTop="true">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableLayout
android:id="@+id/tblLayout"
android:stretchColumns="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
操作栏按钮上的Java代码
bitmap = Bitmap.createBitmap(
tblLayout.getWidth(),
tblLayout.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
tblLayout.draw(c);
Random ran = new Random();
int filename = ran.nextInt();
bitmap = addWaterMark(bitmap);
saveBitmap(bitmap, "" + filename);
private Bitmap addWaterMark(Bitmap src) {
int w = src.getWidth();
int h = src.getHeight();
//Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Bitmap result = Bitmap.createBitmap(w, h,Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Bitmap waterMark = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_logo_full);
waterMark=waterMark.createScaledBitmap(waterMark,150,30,false);
//waterMark.setHeight(30);
//For Right Bottom
//canvas.drawBitmap(bitmap , canvas.getWidth()-bitmap.getWidth() , canvas.getHeight()-bitmap.getHeight() , paint);
w=w/2;
int watermark_width=waterMark.getWidth()/2;
canvas.drawBitmap(waterMark,(w-watermark_width), 10, null);
return result;
}
public void saveBitmap(Bitmap bitmap,String filename) {
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/"+filename+".png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
//bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Log.e("Screenshot", "saved successfully");
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
我尝试将 Bitmap.Config.ARGB_8888 更改为 Bitmap.Config.RGB_565 但是有效但文字图片如下图所示:
在其他应用上分享时,它会正确显示,就像正确显示电子邮件时一样。
已添加
机器人:硬件加速= “假”
和
AndroidManifest中的机器人:largeHeap = “真”
但结果相同