Reputation: 126
I have to write an android application that allows users to add a signature to a document.
I do not know how to do it. Is there an api java that allows to do it or something that I can use in my application?
Upvotes: 0
Views: 1301
Reputation: 1474
What you can do is add a Signature pad below the document like this:-
Step 1) Add this in build.gradel compile 'com.github.gcacace:signature-pad:1.2.0
2) Add this in layout file below your document
<com.github.gcacace.signaturepad.views.SignaturePad
android:id="@+id/signature_pad"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:layout_marginBottom="52dp"
android:background="@drawable/border"
app:penMinWidth="5dp" />
3) Initialize view in android mSignaturePad = (SignaturePad) findViewById(R.id.signature_pad);
4) Create function to save signature as Bitmap createBitmapSvg();
and put this below code
private void createBitmapSvg()
{
Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();
if (addJpgSignatureToGallery(signatureBitmap))
{
//Toast.makeText(AgreementSignature.this, "Signature saved into the Gallery", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(AgreementSignature.this, "Unable to store the signature", Toast.LENGTH_SHORT).show();
}
if (addSvgSignatureToGallery(mSignaturePad.getSignatureSvg()))
{
//Toast.makeText(AgreementSignature.this, "SVG Signature saved into the Gallery", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(AgreementSignature.this, "Unable to store the SVG signature", Toast.LENGTH_SHORT).show();
}
}
public boolean addJpgSignatureToGallery(Bitmap signature) {
boolean result = false;
try {
String path = Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
File photo = new File(Environment.getExternalStorageDirectory() + "/Android/data/" + context.getPackageName() + "/" + String.format("Signature_%d.jpg", System.currentTimeMillis()));
saveBitmapToJPG(signature, photo);
scanMediaFile(photo);
result = true;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private void scanMediaFile(File photo) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(photo);
mediaScanIntent.setData(contentUri);
AgreementSignature.this.sendBroadcast(mediaScanIntent);
}
public boolean addSvgSignatureToGallery(String signatureSvg) {
boolean result = false;
try {
File svgFile = new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.svg", System.currentTimeMillis()));
OutputStream stream = new FileOutputStream(svgFile);
OutputStreamWriter writer = new OutputStreamWriter(stream);
writer.write(signatureSvg);
writer.close();
stream.flush();
stream.close();
scanMediaFile(svgFile);
result = true;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
If you need any more help you can comment here.
Upvotes: 1