我觉得我到处都看了,但我仍然不知道如何实现我的目标。我有一个用 Eclipse 制作的 android 应用程序,它在线性布局中有一个表格布局。该表会进行一些计算并像 Excel 电子表格一样提供答案。我的应用程序的链接如下。我只是想添加一个按钮来导出到可以存储在 SD 驱动器上的图像文件,或者 PDF 文件。我以为这很简单,但我完全迷失在这里。我是编程新手,但该应用程序现在已接近 500 次下载,我已收到添加此功能的请求。我用 XML 显示了 Activity 。感谢您的帮助。 ![在此处输入图片描述][1]
所以我已经有了用 XML (JPEGB) 制作的按钮,如下所示。在 Java 代码中,我还没有添加任何与此按钮或导出到图像相关的内容。 “Button Calculate”用于不同的目的,稍后将在代码中引用。所以基本上,这是从头开始使用 Java 代码,使用 XML 制作的简单按钮。我截断了我认为对这篇文章没有必要的代码。非常感谢!!
Java代码
package com.example.bestserialdilutioncalculator;
import java.math.BigDecimal;
import java.math.RoundingMode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.bestserialdilutioncalculator.R;
public class InitialLayout extends Activity {
Button Calculate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_calculator);
用于布局的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:orientation="vertical"
android:background="#000000"
android:layout_width="42dp"
android:layout_height="fill_parent">
<View
android:layout_width="25dp"
android:layout_centerHorizontal="true"
android:background="@drawable/customicon"
android:layout_height="25dp"
></View>
<Button
android:id="@+id/CalculateButton"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="55dp"
android:background="@drawable/goodbutton"
android:text=" " />
<Button
android:id="@+id/JPEGB"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@drawable/jpegbutton"/>
</RelativeLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollerForTable"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="#000000" >
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/HorizontalScrollerForTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000">
<TableLayout
android:id="@+id/Table"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#000000" >
其余代码是表格布局细节,而且很长。
最佳答案
因此,您可以捕获应用程序的 View (屏幕)并将其写入文件。
首先创建应用程序 Root View 的位图
:
mRootView.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(mRootView.getDrawingCache());
mRootView.setDrawingCacheDisabled(false);
mRootView
将是您在 Activity 的 onCreate() 方法中传递到 setContentView() 的 View 。
然后写入磁盘:
// This should be done off the main (UI) thread!
String externalStoragePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
outFile = new File(externalStoragePath, "myfilename.jpg");
try {
OutputStream os = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
os.flush();
os.close();
} catch (Exception e) {
// handle error here
}
为简洁起见,我没有包含必要的检查以确保外部存储确实可用并已安装。如果需要,我可以添加。
关于android - 如何编程按钮将表格布局导出到 Android 应用程序上的图像或 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15798603/