2

我觉得我已经到处看了,但我仍然不知道如何实现我的目标。我有一个用 Eclipse 制作的 android 应用程序,它在线性布局中有一个表格布局。该表会进行一些计算并提供类似 Excel 电子表格的答案。我的应用程序的链接如下。我只是想添加一个按钮来导出到可以存储在 SD 驱动器上的图像文件,或者 PDF 文件。我以为这很简单,但我完全迷失在这里。我是编程新手,但该应用程序现在有近 500 次下载,我有一个添加此功能的请求。我用 XML 制作了活动显示。感谢您的帮助。![在此处输入图像描述][1]

https://play.google.com/store/apps/details?id=com.bestserialdilutioncalculator&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5iZXN0c2VyaWFsZGlsdXRpb25jYWxjdWxhdG9yIl0

所以我已经有了用 XML (JPEGB) 制作的按钮,如下所示。在 Java 代码中,我还没有添加任何与此按钮或导出到图像相关的内容。“按钮计算”用于不同的目的,稍后将在代码中提及。所以基本上,这是在 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" >

剩下的代码是表格布局细节,而且很长。

4

1 回答 1

2

因此,您可以捕获应用程序的视图(屏幕)并将其写入文件。

首先创建一个Bitmap应用程序的根视图:

mRootView.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(mRootView.getDrawingCache());
mRootView.setDrawingCacheDisabled(false);

mRootView将是您在活动的 onCreate() 方法中传递给 setContentView() 的视图。

然后将其写入磁盘:

// 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
}

为简洁起见,我没有包括确保外部存储确实可用和安装的必要检查。如果需要我可以添加。

于 2013-04-04T01:36:13.207 回答