0

我有一个活动,用户将使用 GestureOverlayView 进行签名。提供签名后,图像将保存在应用程序的内存中,并且引用(图像名称)和其他详细信息将包含在 SQLite 表中。

这是布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#142c57"
    android:padding="3dp"
    tools:context=".SupplierCustomerDetailsSignatureActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3" >

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:descendantFocusability="beforeDescendants"
                    android:focusableInTouchMode="true" >

                    <RelativeLayout
                        android:id="@+id/layout_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:background="@drawable/btn_blue" >

                        <TextView
                            android:id="@+id/customer_name_textView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true"
                            android:text="TextView"
                            android:textColor="#FFFFFF"
                            android:textSize="22sp" />
                    </RelativeLayout>

                    <TableLayout
                        android:id="@+id/table"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/layout_1"
                        android:layout_centerHorizontal="true"
                        android:stretchColumns="0,1" >

                        <TableRow
                            android:id="@+id/tableRow1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:layout_marginTop="10dp">

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="DD Details: "
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />

                            <TextView
                                android:id="@+id/dd_details_textView"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView"
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:layout_marginTop="10dp">

                            <TextView
                                android:id="@+id/textView2"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="DD No.: "
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />

                            <TextView
                                android:id="@+id/dd_no_textView"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView"
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:layout_marginTop="10dp">

                            <TextView
                                android:id="@+id/textView3"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Drawn On: "
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />

                            <TextView
                                android:id="@+id/drawn_on_textView"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView"
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:layout_marginTop="10dp">

                            <TextView
                                android:id="@+id/textView4"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Amount: "
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />

                            <TextView
                                android:id="@+id/amount_textView"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView"
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:layout_marginTop="10dp">

                            <TextView
                                android:id="@+id/textView5"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Type: "
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />

                            <TextView
                                android:id="@+id/type_textView"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView"
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />
                        </TableRow>

                        <TableRow
                            android:id="@+id/tableRow6"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:layout_marginTop="10dp">

                            <TextView
                                android:id="@+id/textView6"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Date Time: "
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />

                            <TextView
                                android:id="@+id/date_time_textView"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView"
                                android:textColor="#FFFFFF"
                                android:textSize="18sp" />
                        </TableRow>
                    </TableLayout>

                    <EditText
                        android:id="@+id/remarks_editText"
                        android:layout_width="500dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/table"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                        android:background="@drawable/gradientbg"
                        android:ems="10"
                        android:hint="Remarks"
                        android:lines="3"
                        android:padding="8dp"
                        android:singleLine="false"
                        android:textColor="#000000"
                        android:textCursorDrawable="@null" >
                    </EditText>

                    <EditText
                        android:id="@+id/receipient_name_editText"
                        android:layout_width="500dp"
                        android:layout_height="50dp"
                        android:layout_below="@+id/remarks_editText"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                        android:background="@drawable/gradientbg"
                        android:ems="10"
                        android:hint="Recepient Name"
                        android:padding="8dp"
                        android:singleLine="true"
                        android:textColor="#000000"
                        android:textCursorDrawable="@null" >
                    </EditText>

                    <RadioGroup
                        android:id="@+id/acceptance_radioGroup"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/receipient_name_editText"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                        android:orientation="horizontal" >

                        <RadioButton
                            android:id="@+id/radio0"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:checked="true"
                            android:text="Accept"
                            android:textColor="#FFFFFF"
                            android:textSize="18sp" />

                        <RadioButton
                            android:id="@+id/radio1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Reject"
                            android:textColor="#FFFFFF"
                            android:textSize="18sp" />
                    </RadioGroup>
                </RelativeLayout>
            </ScrollView>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/scrollView1"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp" >

            <android.gesture.GestureOverlayView
                android:id="@+id/customer_sign_gestures"
                android:layout_width="fill_parent"
                android:layout_height="350dp"
                android:background="#FFFFFF"
                android:fadeEnabled="false"
                android:fadeOffset="5000000000"
                android:gestureColor="#000000"
                android:gestureStrokeType="multiple"
                android:gestureStrokeWidth="3"
                android:uncertainGestureColor="#000000" />

            <TableRow
                android:id="@+id/tableRow7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/customer_sign_gestures"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical|center_horizontal"
                    android:orientation="horizontal" >

                    <Button
                        android:id="@+id/save_button"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5sp"
                        android:background="@drawable/btn_blue"
                        android:text="Save"
                        android:textColor="#FFFFFF" />

                    <Button
                        android:id="@+id/clear_button"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5sp"
                        android:background="@drawable/btn_blue"
                        android:text="Clear"
                        android:textColor="#FFFFFF" />
                </LinearLayout>
            </TableRow>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

这是活动

public class SupplierCustomerDetailsSignatureActivity extends Activity {

    TextView customer_name_textview;
    TextView dd_details_textview;
    TextView dd_no_textview;
    TextView drawn_on_textview;
    TextView amount_textview;
    TextView type_textview;
    TextView date_time_textview;

    GestureOverlayView customer_sign_gesture;

    Button cancel_btn;
    Button save_btn;

    String customersSelected;

    RelativeLayout headerLayout;
    TableRow tableRow1;
    TableRow tableRow2;
    TableRow tableRow3;
    TableRow tableRow4;
    TableRow tableRow5;

    EditText remarks_edittext;
    EditText receipient_name_edittext;

    RadioGroup accept_reject_radio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supplier_customer_details_signature);
        // To remove focus from edittext
        this.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        customer_name_textview = (TextView) findViewById(R.id.customer_name_textView);
        dd_details_textview = (TextView) findViewById(R.id.dd_details_textView);
        dd_no_textview = (TextView) findViewById(R.id.dd_no_textView);
        drawn_on_textview = (TextView) findViewById(R.id.drawn_on_textView);
        amount_textview = (TextView) findViewById(R.id.amount_textView);
        type_textview = (TextView) findViewById(R.id.type_textView);
        date_time_textview = (TextView) findViewById(R.id.date_time_textView);
        customer_sign_gesture = (GestureOverlayView) findViewById(R.id.customer_sign_gestures);
        headerLayout = (RelativeLayout) findViewById(R.id.layout_1);
        tableRow1 = (TableRow) findViewById(R.id.tableRow1);
        tableRow2 = (TableRow) findViewById(R.id.tableRow2);
        tableRow3 = (TableRow) findViewById(R.id.tableRow3);
        tableRow4 = (TableRow) findViewById(R.id.tableRow4);
        tableRow5 = (TableRow) findViewById(R.id.tableRow5);

        remarks_edittext = (EditText) findViewById(R.id.remarks_editText);
        receipient_name_edittext = (EditText) findViewById(R.id.receipient_name_editText);

        accept_reject_radio = (RadioGroup) findViewById(R.id.acceptance_radioGroup);

        customersSelected = getFromPreference("customer_code_clicked");

        if (customersSelected.contains("#@~@#")) {
            headerLayout.setVisibility(View.GONE);
            tableRow1.setVisibility(View.GONE);
            tableRow2.setVisibility(View.GONE);
            tableRow3.setVisibility(View.GONE);
            tableRow4.setVisibility(View.GONE);
            tableRow5.setVisibility(View.GONE);
        }

        customer_name_textview
                .setText(getFromPreference("customer_name_clicked"));
        dd_details_textview.setText("Lore Ipsum");
        dd_no_textview.setText("7894561230");
        drawn_on_textview.setText("State Bank of India");
        amount_textview.setText("Rs. 10,000");
        type_textview.setText("Cheque");

        // makeAToast(customersSelected);

        // setting current date
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df3 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
        String formattedDate = df3.format(c.getTime());
        date_time_textview.setText(formattedDate);

        cancel_btn = (Button) findViewById(R.id.clear_button);
        cancel_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                clearData();
            }
        });

        save_btn = (Button) findViewById(R.id.save_button);
        save_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                saveData();


            }
        });

    }

    @Override
    public void onBackPressed() {
        // do something on back.
        Intent intent = new Intent(
                SupplierCustomerDetailsSignatureActivity.this,
                SupplierDetailsActivity.class);

        finish();
        SupplierCustomerDetailsSignatureActivity.this.startActivity(intent);
        return;
    }

    // method to clear data
    public void clearData() {
        customer_sign_gesture.cancelClearAnimation();
        customer_sign_gesture.clear(true);
    }

    // method to save and send data
    public void saveData() {

        createIfNotExistDraftToSend();


        //checking which radio button is checked
        int index = accept_reject_radio.indexOfChild(findViewById(accept_reject_radio.getCheckedRadioButtonId()));
        String acceptReject;
        if (index ==0)
        {
            acceptReject ="accept";
        }
        else
        {
            acceptReject ="reject";
        }

        // checking whether edittext contains values
        if (remarks_edittext.getText().toString().trim().length() > 0
                && receipient_name_edittext.getText().toString().trim()
                        .length() > 0) {


            // checking whether there is multiple id
            if (customersSelected.contains("#@~@#")) {
                Log.i("Full String ", customersSelected);
                String assetClasses = customersSelected;
                String[] splits = assetClasses.split("#@~@#");
                for (int k = 1; k < splits.length; k++) {
                    Log.i("Values", splits[k]);

                    // getting image
                    Bitmap gestureImg = customer_sign_gesture.getGesture().toBitmap(100, 100,
                            8, Color.BLACK);

                    String image = saveImageToInternalSorage(gestureImg,splits[k], remarks_edittext.getText().toString(),
                            receipient_name_edittext.getText().toString(), acceptReject, date_time_textview.getText().toString());
                    Log.i("Image:", image);

                }
            } else {

            }

        } else {
            makeAToast("Please enter credentials!");
        }

    }

    // Internal storage
        private String saveImageToInternalSorage(Bitmap bitmapImage, String cl_doc_no, String remarks,
                String recepient,  String ar, String mobdttm) {
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            // path to /data/data/yourapp/app_data/imageDir
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);

            Random gen = new Random();
            int n = 10000;
            n = gen.nextInt(n);
            String fotoname = "Photo-" + n + ".png";
            Log.i("Photo Name:", fotoname);
            // Create imageDir
            File mypath = new File(directory, fotoname);

            FileOutputStream fos = null;
            try {

                fos = new FileOutputStream(mypath);

                // Use the compress method on the BitMap object to write image to
                // the OutputStream
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            //inserting in local database
            insertDraftToSend(cl_doc_no, remarks,
                    recepient, fotoname, ar, mobdttm);
            //saveInPreference("Photo",fotoname);
            return directory.getAbsolutePath();
        }

    // ===============Local Database Handling Methods===================

    // method to create If Not Exist
    public void createIfNotExistDraftToSend() {
        DatabaseHandler db = new DatabaseHandler(
                SupplierCustomerDetailsSignatureActivity.this);
        ContentValues values = new ContentValues();

        db.createIfNotExistDraftToSend();
        db.close();
    }

    // method to insert data
    public void insertDraftToSend(String cl_doc_no, String remarks,
            String recepient, String signature, String ar, String mobdttm) {
        DatabaseHandler db = new DatabaseHandler(
                SupplierCustomerDetailsSignatureActivity.this);
        ContentValues values = new ContentValues();
        // db.createDataBase();
        values.put("cl_doc_no", cl_doc_no);
        values.put("remarks", remarks);
        values.put("recepient", recepient);
        values.put("signature", signature);
        values.put("ar", ar);
        values.put("mobdttm", mobdttm);
        db.insertDraftToSend(cl_doc_no, remarks, recepient, signature, ar,
                mobdttm);
        db.close();

    }

    // ===============Local Database Handling Methods===================

    // method to save variable in preference
    public void saveInPreference(String name, String content) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(name, content);
        editor.commit();
    }

    // getting content from preferences
    public String getFromPreference(String variable_name) {
        String preference_return;
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        preference_return = preferences.getString(variable_name, "");

        return preference_return;
    }

    // method to show toast message
    public void makeAToast(String str) {
        // yet to implement
        Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


}

我面临的问题是,如果用户提供签名,那么所需的数据将被插入到本地数据库中,一切都很好,但是如果用户将签名区域保持为空白,那么应用程序将由于以下原因而强制关闭空指针异常。请建议我在插入数据之前如何知道签名区域或 GestureOverlayView 是否为空白。

4

1 回答 1

0

试试这个方法来获取图像:

customer_sign_gesture.setDrawingCacheEnabled(true);
Bitmap gestureImg = Bitmap.createBitmap(customer_sign_gesture.getDrawingCache());
于 2013-10-08T08:29:59.293 回答