+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ super.onCreateOptionsMenu(menu);
+ menu.add(0, Menu.FIRST, 0, R.string.menu_share).setIcon(android.R.drawable.ic_menu_share);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (qrCodeEncoder == null) { // Odd
+ Log.w(TAG, "No existing barcode to send?");
+ return true;
+ }
+
+ String contents = qrCodeEncoder.getContents();
+ Bitmap bitmap;
+ try {
+ bitmap = QRCodeEncoder.encodeAsBitmap(contents, BarcodeFormat.QR_CODE,
+ SHARE_BARCODE_DIMENSION, SHARE_BARCODE_DIMENSION);
+ } catch (WriterException we) {
+ Log.w(TAG, we);
+ return true;
+ }
+
+ File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
+ File barcodesRoot = new File(bsRoot, "Barcodes");
+ if (!barcodesRoot.exists() && !barcodesRoot.mkdirs()) {
+ Log.w(TAG, "Couldn't make dir " + barcodesRoot);
+ showErrorMessage(R.string.msg_unmount_usb);
+ return true;
+ }
+ File barcodeFile = new File(barcodesRoot, makeBarcodeFileName(contents) + ".png");
+ barcodeFile.delete();
+ FileOutputStream fos = null;
+ try {
+ fos = new FileOutputStream(barcodeFile);
+ bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
+ } catch (FileNotFoundException fnfe) {
+ Log.w(TAG, "Couldn't access file " + barcodeFile + " due to " + fnfe);
+ showErrorMessage(R.string.msg_unmount_usb);
+ return true;
+ } finally {
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (IOException ioe) {
+ // do nothing
+ }
+ }
+ }
+
+ Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
+ intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " - " +
+ qrCodeEncoder.getTitle());
+ intent.putExtra(Intent.EXTRA_TEXT, qrCodeEncoder.getContents());
+ intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + barcodeFile.getAbsolutePath()));
+ intent.setType("image/png");
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+ startActivity(Intent.createChooser(intent, null));
+ return true;
+ }
+
+ private static CharSequence makeBarcodeFileName(CharSequence contents) {
+ int fileNameLength = Math.min(MAX_BARCODE_FILENAME_LENGTH, contents.length());
+ StringBuilder fileName = new StringBuilder(fileNameLength);
+ for (int i = 0; i < fileNameLength; i++) {
+ char c = contents.charAt(i);
+ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
+ fileName.append(c);
+ } else {
+ fileName.append('_');
+ }
+ }
+ return fileName;
+ }
+