// It would be nice if the string encoding lived in the core ZXing library, // but we use platform specific code like PhoneNumberUtils, so it can't. private boolean encodeContentsFromZXingIntent(Intent intent) { // Default to QR_CODE if no format given. String formatString = intent.getStringExtra(Intents.Encode.FORMAT); format = null; if (formatString != null) { try { format = BarcodeFormat.valueOf(formatString); } catch (IllegalArgumentException iae) { // Ignore it then } } if (format == null || format == BarcodeFormat.QR_CODE) { String type = intent.getStringExtra(Intents.Encode.TYPE); if (type == null || type.isEmpty()) { return false; } this.format = BarcodeFormat.QR_CODE; encodeQRCodeContents(intent, type); } else { String data = intent.getStringExtra(Intents.Encode.DATA); if (data != null && !data.isEmpty()) { contents = data; displayContents = data; title = activity.getString(R.string.contents_text); } } return contents != null && !contents.isEmpty(); }
// Handles send intents from the Contacts app, retrieving a contact as a VCARD. private void encodeFromStreamExtra(Intent intent) throws WriterException { format = BarcodeFormat.QR_CODE; Bundle bundle = intent.getExtras(); if (bundle == null) { throw new WriterException("No extras"); } Uri uri = bundle.getParcelable(Intent.EXTRA_STREAM); if (uri == null) { throw new WriterException("No EXTRA_STREAM"); } byte[] vcard; String vcardString; InputStream stream = null; try { stream = activity.getContentResolver().openInputStream(uri); if (stream == null) { throw new WriterException("Can't open stream for " + uri); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = stream.read(buffer)) > 0) { baos.write(buffer, 0, bytesRead); } vcard = baos.toByteArray(); vcardString = new String(vcard, 0, vcard.length, "UTF-8"); } catch (IOException ioe) { throw new WriterException(ioe); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // continue } } } Log.d(TAG, "Encoding share intent content:"); Log.d(TAG, vcardString); Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE); ParsedResult parsedResult = ResultParser.parseResult(result); if (!(parsedResult instanceof AddressBookParsedResult)) { throw new WriterException("Result was not an address"); } encodeQRCodeContents((AddressBookParsedResult) parsedResult); if (contents == null || contents.isEmpty()) { throw new WriterException("No content to encode"); } }