@Override protected void onResume() { super.onResume(); // This assumes the view is full screen, which is a good assumption WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); int smallerDimension = width < height ? width : height; smallerDimension = smallerDimension * 7 / 8; Intent intent = getIntent(); try { qrCodeEncoder = new QRCodeEncoder(this, intent, smallerDimension); setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle()); Bitmap bitmap = qrCodeEncoder.encodeAsBitmap(); ImageView view = (ImageView) findViewById(R.id.image_view); view.setImageBitmap(bitmap); TextView contents = (TextView) findViewById(R.id.contents_text_view); contents.setText(qrCodeEncoder.getDisplayContents()); } catch (WriterException e) { Log.e(TAG, "Could not encode barcode", e); showErrorMessage(R.string.msg_encode_contents_failed); qrCodeEncoder = null; } catch (IllegalArgumentException e) { Log.e(TAG, "Could not encode barcode", e); showErrorMessage(R.string.msg_encode_contents_failed); qrCodeEncoder = null; } }
private void share() { QRCodeEncoder encoder = qrCodeEncoder; if (encoder == null) { // Odd Log.w(TAG, "No existing barcode to send?"); return; } String contents = encoder.getContents(); if (contents == null) { Log.w(TAG, "No existing barcode to send?"); return; } Bitmap bitmap; try { bitmap = encoder.encodeAsBitmap(); } catch (WriterException we) { Log.w(TAG, we); return; } if (bitmap == null) { return; } 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; } 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; } 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) + " - " + encoder.getTitle()); intent.putExtra(Intent.EXTRA_TEXT, contents); 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)); }
@Override protected void onResume() { super.onResume(); // This assumes the view is full screen, which is a good assumption WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); Point displaySize = new Point(); display.getSize(displaySize); int width = displaySize.x; int height = displaySize.y; int smallerDimension = width < height ? width : height; smallerDimension = smallerDimension * 7 / 8; Intent intent = getIntent(); if (intent == null) { return; } try { boolean useVCard = intent.getBooleanExtra(USE_VCARD_KEY, false); qrCodeEncoder = new QRCodeEncoder(this, intent, smallerDimension, useVCard); Bitmap bitmap = qrCodeEncoder.encodeAsBitmap(); if (bitmap == null) { Log.w(TAG, "Could not encode barcode"); showErrorMessage(R.string.msg_encode_contents_failed); qrCodeEncoder = null; return; } ImageView view = (ImageView) findViewById(R.id.image_view); view.setImageBitmap(bitmap); TextView contents = (TextView) findViewById(R.id.contents_text_view); if (intent.getBooleanExtra(Intents.Encode.SHOW_CONTENTS, true)) { contents.setText(qrCodeEncoder.getDisplayContents()); setTitle(qrCodeEncoder.getTitle()); } else { contents.setText(""); setTitle(""); } } catch (WriterException e) { Log.w(TAG, "Could not encode barcode", e); showErrorMessage(R.string.msg_encode_contents_failed); qrCodeEncoder = null; } }
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(Menu.NONE, SHARE_MENU, Menu.NONE, R.string.menu_share) .setIcon(android.R.drawable.ic_menu_share); int encodeNameResource = qrCodeEncoder.isUseVCard() ? R.string.menu_encode_mecard : R.string.menu_encode_vcard; menu.add(Menu.NONE, ENCODE_FORMAT_MENU, Menu.NONE, encodeNameResource) .setIcon(android.R.drawable.ic_menu_sort_alphabetically); return true; }
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.encode, menu); boolean useVcard = qrCodeEncoder != null && qrCodeEncoder.isUseVCard(); int encodeNameResource = useVcard ? R.string.menu_encode_mecard : R.string.menu_encode_vcard; MenuItem encodeItem = menu.findItem(R.id.menu_encode); encodeItem.setTitle(encodeNameResource); Intent intent = getIntent(); if (intent != null) { String type = intent.getStringExtra(Intents.Encode.TYPE); encodeItem.setVisible(Contents.Type.CONTACT.equals(type)); } return super.onCreateOptionsMenu(menu); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case SHARE_MENU: share(); return true; case ENCODE_FORMAT_MENU: Intent intent = getIntent(); intent.putExtra(USE_VCARD_KEY, !qrCodeEncoder.isUseVCard()); startActivity(getIntent()); finish(); return true; default: return false; } }
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_share) { share(); return true; } else if (item.getItemId() == R.id.menu_encode) { Intent intent = getIntent(); if (intent == null) { return false; } intent.putExtra(USE_VCARD_KEY, !qrCodeEncoder.isUseVCard()); startActivity(intent); finish(); return true; } else { return false; } }
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == RHelper.getId("menu_share")) { share(); return true; } else if (item.getItemId() == RHelper.getId("menu_encode")) { Intent intent = getIntent(); if (intent == null) { return false; } intent.putExtra(USE_VCARD_KEY, !qrCodeEncoder.isUseVCard()); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); return true; } else { return false; } }
@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(); } catch (WriterException we) { Log.w(TAG, we); return true; } if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 保存图片到SD上 File sdCardDir = Environment.getExternalStorageDirectory(); sdCardDir = new File(sdCardDir, "TwoCodeContact"); FileOutputStream fos = null; try { if (!sdCardDir.exists()) { sdCardDir.mkdir(); } File saveFile = new File(sdCardDir, makeBarcodeFileName(contents) + ".png"); Log.i(TAG, saveFile.getAbsolutePath()); fos = new FileOutputStream(saveFile); bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos); fos.flush(); fos.close(); } catch (FileNotFoundException fnfe) { Log.w(TAG, "找不到文件 due to " + fnfe); showErrorMessage(R.string.msg_unmount_usb); return true; } catch (IOException e) { Log.e(TAG, "文件读写出现异常情况"); if (fos != null) { try { fos.close(); } catch (IOException ioe) { } } } finally { if (fos != null) { try { fos.close(); } catch (IOException ioe) { } } } } // File bsRoot = new File(Environment.getExternalStorageDirectory(), "TwoCodeContact"); // File barcodesRoot = new File(bsRoot, "TwoCodeContact"); // if (!barcodesRoot.exists() && !barcodesRoot.mkdirs()) { // Log.w(TAG, "���ܴ����ļ��� " + 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(nFile); // bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos); // Toast.makeText(this, "��ɶ�ά��ɹ�!", Toast.LENGTH_SHORT); // } catch (FileNotFoundException fnfe) { // Log.w(TAG, "���ܷ����ļ� " + nFile + " due to " + fnfe); // showErrorMessage(R.string.msg_unmount_usb); // return true; // } finally { // if (fos != null) { // try { // fos.close(); // } catch (IOException ioe) { // } // } // } Intent intent = new Intent(this, ContactDetailActivity.class); intent.putExtra("ContactInfo", contact); startActivity(intent); finish(); // ���?SMS���ͣ������ŵ�Activity // 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; }