0

The text works but the image does not, by that I mean that when I pull up the email, everything in the .putExtra(Intent.EXTRA_TEXT.... is there and in working order. It says unable to attach file. file size 0 bytes.

public class DigitalSignature extends AppCompatActivity {
String pilot, ship, to, from, zone1, zone2, CallSign, agent, date;
Toolbar toolbar;
Button btn_get_sign, mClear, mGetSign, mCancel, btn_send;
double vesselUnit;
File file;
Dialog dialog;
LinearLayout mContent;
View view;
signature mSignature;
Bitmap bitmap;

// Creating Separate Directory for saving Generated Images
String DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/DigitSign/";
String pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String StoredPath = DIRECTORY + pic_name + ".png";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_digital_signature);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        pilot = extras.getString("Pilot");
        ship = extras.getString("ship");
        to = extras.getString("to");
        from = extras.getString("from");
        zone1 = extras.getString("zone1");
        zone2 = extras.getString("zone2");
        CallSign = extras.getString("callsign");
        agent = extras.getString("agent");
        vesselUnit = extras.getDouble("vesselunits");
        date = extras.getString("date");
    }
    // Setting ToolBar as ActionBar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Button to open signature panel
    btn_get_sign = (Button) findViewById(R.id.signature);
    btn_send= (Button) findViewById(R.id.btn_send);
    // Method to create Directory, if the Directory doesn't exists
    file = new File(DIRECTORY);
    boolean isDirectoryCreated = file.exists();
    if (!isDirectoryCreated) {
        isDirectoryCreated = file.mkdir();
    }

    // Dialog Function
    dialog = new Dialog(this);
    // Removing the features of Normal Dialogs
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_sig);
    dialog.setCancelable(true);

    btn_get_sign.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Function call for Digital Signature
            dialog_action();

        }
    });
    btn_send.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            Uri path = Uri.parse("file://" + file);
            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.putExtra(Intent.EXTRA_TEXT, pilot+"\n"+ship+"\n"+to+"\n"+from+"\n"+zone1+"\n"+zone2+"\n"+CallSign+"\n"+agent+"\n"+vesselUnit);
            // set the type to 'email'
            emailIntent.setType("image/png");
            String to[] = {"hidingMyEMail"};
            emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
            // the attachment
            emailIntent.putExtra(Intent.EXTRA_STREAM, path);
            // the mail subject
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, pilot+"'s Ticket for "+ship);
            startActivity(Intent.createChooser(emailIntent , "Send email..."));
        }
    });
}

// Function for Digital Signature
public void dialog_action() {

    mContent = (LinearLayout) dialog.findViewById(R.id.linearLayout);
    mSignature = new signature(getApplicationContext(), null);
    mSignature.setBackgroundColor(Color.WHITE);
    // Dynamically generating Layout through java code
    mContent.addView(mSignature, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    mClear = (Button) dialog.findViewById(R.id.clear);
    mGetSign = (Button) dialog.findViewById(R.id.getsign);
    mGetSign.setEnabled(false);
    mCancel = (Button) dialog.findViewById(R.id.cancel);
    view = mContent;

    mClear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v("tag", "Panel Cleared");
            mSignature.clear();
            mGetSign.setEnabled(false);
        }
    });
    mGetSign.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Log.v("tag", "Panel Saved");
            view.setDrawingCacheEnabled(true);
            mSignature.save(view, StoredPath);
            dialog.dismiss();
            Toast.makeText(getApplicationContext(), "Successfully Saved", Toast.LENGTH_SHORT).show();
            // Calling the same class
            recreate();
        }
    });
    mCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v("tag", "Panel Cancelled");
            dialog.dismiss();
            // Calling the same class
            recreate();
        }
    });
    dialog.show();
}

public class signature extends View {
    private static final float STROKE_WIDTH = 5f;
    private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
    private Paint paint = new Paint();
    private Path path = new Path();

    private float lastTouchX;
    private float lastTouchY;
    private final RectF dirtyRect = new RectF();

    public signature(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
    }

    public void save(View v, String StoredPath) {
        Log.v("tag", "Width: " + v.getWidth());
        Log.v("tag", "Height: " + v.getHeight());
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);
        }
        Canvas canvas = new Canvas(bitmap);
        try {
            // Output the file
            FileOutputStream mFileOutStream = new FileOutputStream(StoredPath);
            v.draw(canvas);
            // Convert the output file to Image such as .png
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
            mFileOutStream.flush();
            mFileOutStream.close();
        } catch (Exception e) {
            Log.v("log_tag", e.toString());
        }
    }

    public void clear() {
        path.reset();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        mGetSign.setEnabled(true);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                lastTouchX = eventX;
                lastTouchY = eventY;
                return true;

            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_UP:
                resetDirtyRect(eventX, eventY);
                int historySize = event.getHistorySize();
                for (int i = 0; i < historySize; i++) {
                    float historicalX = event.getHistoricalX(i);
                    float historicalY = event.getHistoricalY(i);
                    expandDirtyRect(historicalX, historicalY);
                    path.lineTo(historicalX, historicalY);
                }
                path.lineTo(eventX, eventY);
                break;
            default:
                debug("Ignored touch event: " + event.toString());
                return false;
        }

        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
                (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
    }

    private void debug(String string) {
        Log.v("log_tag", string);
    }

    private void expandDirtyRect(float historicalX, float historicalY) {
        if (historicalX < dirtyRect.left) {
            dirtyRect.left = historicalX;
        } else if (historicalX > dirtyRect.right) {
            dirtyRect.right = historicalX;
        }

        if (historicalY < dirtyRect.top) {
            dirtyRect.top = historicalY;
        } else if (historicalY > dirtyRect.bottom) {
            dirtyRect.bottom = historicalY;
        }
    }

    private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
    }
}
  • This should work: Uri path = Uri.parse("file://" + file.getAbsolutePath()); – Asama Jul 15 '17 at 21:46
  • @Asama that didn't change anything :/ – Andrew McLaren Jul 15 '17 at 22:05
  • Have you read [this](https://developer.android.com/training/sharing/send.html#send-binary-content) doc? It states: "The receiving application needs permission to access the data the Uri points to", so just make sure it does. Also take a look at these answers: https://stackoverflow.com/a/20333318/2776632 https://stackoverflow.com/a/7919230/2776632 – Diego dos Santos Jul 17 '17 at 19:51
  • @DiegodosSantos I rewrote this code using fileprovider but I'm having a syntax issue of you could take a look that would be awesome https://stackoverflow.com/questions/45269953/android-fileprovider-syntax – Andrew McLaren Jul 25 '17 at 00:07

0 Answers0