List of usage examples for android.database Cursor getBlob
byte[] getBlob(int columnIndex);
From source file:com.phonegap.plugins.sqlitePlugin.SQLitePlugin.java
/** * Process query results./*from www. j a va 2s. c om*/ * * @param cur * Cursor into query results * @param tx_id * Transaction id */ public void processResults(Cursor cur, String query_id, String tx_id) { String result = "[]"; // If query result has rows if (cur.moveToFirst()) { JSONArray fullresult = new JSONArray(); String key = ""; int colCount = cur.getColumnCount(); // Build up JSON result object for each row do { JSONObject row = new JSONObject(); try { for (int i = 0; i < colCount; ++i) { key = cur.getColumnName(i); if (android.os.Build.VERSION.SDK_INT >= 11) { switch (cur.getType(i)) { case Cursor.FIELD_TYPE_NULL: row.put(key, null); break; case Cursor.FIELD_TYPE_INTEGER: row.put(key, cur.getInt(i)); break; case Cursor.FIELD_TYPE_FLOAT: row.put(key, cur.getFloat(i)); break; case Cursor.FIELD_TYPE_STRING: row.put(key, cur.getString(i)); break; case Cursor.FIELD_TYPE_BLOB: row.put(key, cur.getBlob(i)); break; } } else { row.put(key, cur.getString(i)); } } fullresult.put(row); } catch (JSONException e) { e.printStackTrace(); } } while (cur.moveToNext()); result = fullresult.toString(); } if (query_id.length() > 0) this.sendJavascript(" SQLitePluginTransaction.queryCompleteCallback('" + tx_id + "','" + query_id + "', " + result + ");"); }
From source file:com.robotoworks.mechanoid.db.SQuery.java
public List<byte[]> selectBlobList(Uri uri, String column, String orderBy) { Cursor cursor = null; try {//from w w w .j a v a 2 s . co m cursor = select(uri, new String[] { column }, orderBy, false); List<byte[]> list = new ArrayList<byte[]>(cursor.getCount()); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToNext(); list.add(cursor.getBlob(0)); } return list; } finally { Closeables.closeSilently(cursor); } }
From source file:org.thialfihar.android.apg.provider.ProviderHelper.java
/** * TODO: currently not used, but will be needed to upload many keys at once! * * @param masterKeyIds/* ww w . ja v a2s.com*/ * @return * @throws IOException */ public ArrayList<String> getKeyRingsAsArmoredString(long[] masterKeyIds) throws IOException { ArrayList<String> output = new ArrayList<String>(); if (masterKeyIds == null || masterKeyIds.length == 0) { Log.e(Constants.TAG, "No master keys given!"); return output; } // Build a cursor for the selected masterKeyIds Cursor cursor; { String inMasterKeyList = KeyRingData.MASTER_KEY_ID + " IN ("; for (int i = 0; i < masterKeyIds.length; ++i) { if (i != 0) { inMasterKeyList += ", "; } inMasterKeyList += DatabaseUtils.sqlEscapeString("" + masterKeyIds[i]); } inMasterKeyList += ")"; cursor = mContentResolver.query(KeyRingData.buildPublicKeyRingUri(), new String[] { KeyRingData._ID, KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA }, inMasterKeyList, null, null); } try { if (cursor != null) { int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID); int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); if (cursor.moveToFirst()) { do { Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol)); byte[] data = cursor.getBlob(dataCol); // get actual keyring data blob and write it to ByteArrayOutputStream try { output.add(getKeyRingAsArmoredString(data)); } catch (IOException e) { Log.e(Constants.TAG, "IOException", e); } } while (cursor.moveToNext()); } } } finally { if (cursor != null) { cursor.close(); } } if (output.size() > 0) { return output; } else { return null; } }
From source file:com.digicorp.plugin.sqlitePlugin.SQLitePlugin.java
/** * Convert results cursor to JSON string. * * @param cur/*ww w . j av a2s .c o m*/ * Cursor into query results * * @return results in string form * */ @SuppressLint("NewApi") private String results2string(Cursor cur) { String result = "[]"; // If query result has rows if (cur.moveToFirst()) { JSONArray fullresult = new JSONArray(); String key = ""; int colCount = cur.getColumnCount(); // Build up JSON result object for each row do { JSONObject row = new JSONObject(); try { for (int i = 0; i < colCount; ++i) { key = cur.getColumnName(i); // for old Android SDK remove lines from HERE: if (android.os.Build.VERSION.SDK_INT >= 11) { switch (cur.getType(i)) { case Cursor.FIELD_TYPE_NULL: row.put(key, null); break; case Cursor.FIELD_TYPE_INTEGER: row.put(key, cur.getInt(i)); break; case Cursor.FIELD_TYPE_FLOAT: row.put(key, cur.getFloat(i)); break; case Cursor.FIELD_TYPE_STRING: row.put(key, cur.getString(i)); break; case Cursor.FIELD_TYPE_BLOB: row.put(key, new String(Base64.encode(cur.getBlob(i), Base64.DEFAULT))); break; } } else // to HERE. { row.put(key, cur.getString(i)); } } fullresult.put(row); } catch (JSONException e) { e.printStackTrace(); } } while (cur.moveToNext()); result = fullresult.toString(); } return result; }
From source file:com.zetaDevelopment.phonegap.plugin.sqlitePlugin.SQLitePlugin.java
/** * Convert results cursor to JSON string. * * @param cur//from w w w .j ava 2 s .c om * Cursor into query results * * @return results in string form * */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private String results2string(Cursor cur) { String result = "[]"; // If query result has rows if (cur.moveToFirst()) { JSONArray fullresult = new JSONArray(); String key = ""; int colCount = cur.getColumnCount(); // Build up JSON result object for each row do { JSONObject row = new JSONObject(); try { for (int i = 0; i < colCount; ++i) { key = cur.getColumnName(i); // for old Android SDK remove lines from HERE: if (android.os.Build.VERSION.SDK_INT >= 11) { switch (cur.getType(i)) { case Cursor.FIELD_TYPE_NULL: row.put(key, null); break; case Cursor.FIELD_TYPE_INTEGER: row.put(key, cur.getInt(i)); break; case Cursor.FIELD_TYPE_FLOAT: row.put(key, cur.getFloat(i)); break; case Cursor.FIELD_TYPE_STRING: row.put(key, cur.getString(i)); break; case Cursor.FIELD_TYPE_BLOB: //row.put(key, cur.getBlob(i)); row.put(key, new String(Base64.encode(cur.getBlob(i), Base64.DEFAULT))); break; } } else // to HERE. { row.put(key, cur.getString(i)); } } fullresult.put(row); } catch (JSONException e) { e.printStackTrace(); } } while (cur.moveToNext()); result = fullresult.toString(); } return result; }
From source file:co.nerdart.ourss.adapter.FeedsCursorAdapter.java
@Override protected void bindChildView(View view, Context context, Cursor cursor) { view.findViewById(R.id.indicator).setVisibility(View.INVISIBLE); TextView textView = ((TextView) view.findViewById(android.R.id.text1)); long feedId = cursor.getLong(idPosition); if (feedId == mSelectedFeedId) { view.setBackgroundResource(android.R.color.holo_blue_dark); } else {/*from w ww.j av a 2 s .c o m*/ view.setBackgroundResource(android.R.color.transparent); } TextView updateTextView = ((TextView) view.findViewById(android.R.id.text2)); updateTextView.setVisibility(View.VISIBLE); if (cursor.isNull(errorPosition)) { long timestamp = cursor.getLong(lastUpdateColumn); // Date formatting is expensive, look at the cache String formattedDate = mFormattedDateCache.get(timestamp); if (formattedDate == null) { Date date = new Date(timestamp); formattedDate = context.getString(R.string.update) + COLON + (timestamp == 0 ? context.getString(R.string.never) : new StringBuilder(Constants.DATE_FORMAT.format(date)).append(' ') .append(Constants.TIME_FORMAT.format(date))); mFormattedDateCache.put(timestamp, formattedDate); } updateTextView.setText(formattedDate); } else { updateTextView.setText(new StringBuilder(context.getString(R.string.error)).append(COLON) .append(cursor.getString(errorPosition))); } byte[] iconBytes = cursor.getBlob(iconPosition); if (iconBytes != null && iconBytes.length > 0) { Bitmap bitmap = BitmapFactory.decodeByteArray(iconBytes, 0, iconBytes.length); if (bitmap != null && bitmap.getHeight() > 0 && bitmap.getWidth() > 0) { int bitmapSizeInDip = UiUtils.dpToPixel(18); if (bitmap.getHeight() != bitmapSizeInDip) { bitmap = Bitmap.createScaledBitmap(bitmap, bitmapSizeInDip, bitmapSizeInDip, false); } textView.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(context.getResources(), bitmap), null, null, null); } else { textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } } else { view.setTag(null); textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } int unreadCount; synchronized (mUnreadItemsByFeed) { unreadCount = mUnreadItemsByFeed.get(feedId); } if (unreadCount > 0) { textView.setEnabled(true); updateTextView.setEnabled(true); } else { textView.setEnabled(false); updateTextView.setEnabled(false); } textView.setText( (cursor.isNull(namePosition) ? cursor.getString(linkPosition) : cursor.getString(namePosition)) + (unreadCount > 0 ? " (" + unreadCount + ")" : "")); View sortView = view.findViewById(R.id.sortitem); if (!sortViews.contains(sortView)) { // as we are reusing views, this is fine sortViews.add(sortView); } sortView.setVisibility(feedSort ? View.VISIBLE : View.GONE); }
From source file:learn2crack.activities.WnContactsListFragment.java
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { array.clear();/*www .j av a 2 s . c o m*/ list.clear(); // This swaps the new cursor into the adapter. while (data.moveToNext()) { long id = data.getLong(ContactsQuery.idIdx); Contact currentContact = array.get(id); if (currentContact == null) { if (!(data.getString(ContactsQuery.accountTypeIdx)).equals("learn2crack.chat.account")) { continue; } currentContact = new Contact(id, data.getString(ContactsQuery.nameIdx), getResources()); array.put(id, currentContact); list.add(currentContact); } int type = data.getInt(ContactsQuery.typeIdx); String mimeType = data.getString(ContactsQuery.mimeTypeIdx); if (mimeType.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) { String phone = data.getString(ContactsQuery.dataIdx); currentContact.addPhone(type, phone); } else { // mimeType == ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE //String photo = data.getString(ContactsQuery.dataIdx); byte[] photo = data.getBlob(ContactsQuery.photoIdx); if (photo != null) { currentContact.setPhoto(BitmapFactory.decodeByteArray(photo, 0, photo.length)); } } } data.close(); mAdapter.notifyDataSetChanged(); }
From source file:org.sufficientlysecure.keychain.ui.keyview.ViewKeyActivity.java
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { /* TODO better error handling? May cause problems when a key is deleted, * because the notification triggers faster than the activity closes. *///from w w w.j a va 2 s . c om // Swap the new cursor in. (The framework will take care of closing the // old cursor once we return.) switch (loader.getId()) { case LOADER_ID_UNIFIED: { // Avoid NullPointerExceptions... if (data.getCount() == 0) { return; } if (data.moveToFirst()) { // get name, email, and comment from USER_ID String name = data.getString(INDEX_NAME); mCollapsingToolbarLayout.setTitle(name != null ? name : getString(R.string.user_id_no_name)); mMasterKeyId = data.getLong(INDEX_MASTER_KEY_ID); mFingerprint = data.getBlob(INDEX_FINGERPRINT); mIsSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0; mHasEncrypt = data.getInt(INDEX_HAS_ENCRYPT) != 0; mIsRevoked = data.getInt(INDEX_IS_REVOKED) > 0; mIsExpired = data.getInt(INDEX_IS_EXPIRED) != 0; mIsSecure = data.getInt(INDEX_IS_SECURE) == 1; mIsVerified = data.getInt(INDEX_VERIFIED) > 0; // queue showing of the main fragment showMainFragment(); // if the refresh animation isn't playing if (!mRotate.hasStarted() && !mRotateSpin.hasStarted()) { // re-create options menu based on mIsSecret, mIsVerified supportInvalidateOptionsMenu(); // this is done at the end of the animation otherwise } AsyncTask<Long, Void, Bitmap> photoTask = new AsyncTask<Long, Void, Bitmap>() { protected Bitmap doInBackground(Long... mMasterKeyId) { return new ContactHelper(ViewKeyActivity.this).loadPhotoByMasterKeyId(mMasterKeyId[0], true); } protected void onPostExecute(Bitmap photo) { if (photo == null) { return; } mPhoto.setImageBitmap(photo); mPhoto.setColorFilter(getResources().getColor(R.color.toolbar_photo_tint), PorterDuff.Mode.SRC_ATOP); mPhotoLayout.setVisibility(View.VISIBLE); } }; boolean showStatusText = mIsSecure && !mIsExpired && !mIsRevoked; if (showStatusText) { mStatusText.setVisibility(View.VISIBLE); if (mIsSecret) { mStatusText.setText(R.string.view_key_my_key); } else if (mIsVerified) { mStatusText.setText(R.string.view_key_verified); } else { mStatusText.setText(R.string.view_key_unverified); } } else { mStatusText.setVisibility(View.GONE); } // Note: order is important int color; if (mIsRevoked) { mStatusImage.setVisibility(View.VISIBLE); KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText, State.REVOKED, R.color.icons, true); // noinspection deprecation, fix requires api level 23 color = getResources().getColor(R.color.key_flag_red); mActionEncryptFile.setVisibility(View.INVISIBLE); mActionEncryptText.setVisibility(View.INVISIBLE); hideFab(); mQrCodeLayout.setVisibility(View.GONE); } else if (!mIsSecure) { mStatusImage.setVisibility(View.VISIBLE); KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText, State.INSECURE, R.color.icons, true); // noinspection deprecation, fix requires api level 23 color = getResources().getColor(R.color.key_flag_red); mActionEncryptFile.setVisibility(View.INVISIBLE); mActionEncryptText.setVisibility(View.INVISIBLE); hideFab(); mQrCodeLayout.setVisibility(View.GONE); } else if (mIsExpired) { mStatusImage.setVisibility(View.VISIBLE); KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText, State.EXPIRED, R.color.icons, true); // noinspection deprecation, fix requires api level 23 color = getResources().getColor(R.color.key_flag_red); mActionEncryptFile.setVisibility(View.INVISIBLE); mActionEncryptText.setVisibility(View.INVISIBLE); hideFab(); mQrCodeLayout.setVisibility(View.GONE); } else if (mIsSecret) { mStatusImage.setVisibility(View.GONE); // noinspection deprecation, fix requires api level 23 color = getResources().getColor(R.color.key_flag_green); // reload qr code only if the fingerprint changed if (!Arrays.equals(mFingerprint, mQrCodeLoaded)) { loadQrCode(mFingerprint); } photoTask.execute(mMasterKeyId); mQrCodeLayout.setVisibility(View.VISIBLE); // and place leftOf qr code // RelativeLayout.LayoutParams nameParams = (RelativeLayout.LayoutParams) // mName.getLayoutParams(); // // remove right margin // nameParams.setMargins(FormattingUtils.dpToPx(this, 48), 0, 0, 0); // if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // nameParams.setMarginEnd(0); // } // nameParams.addRule(RelativeLayout.LEFT_OF, R.id.view_key_qr_code_layout); // mName.setLayoutParams(nameParams); RelativeLayout.LayoutParams statusParams = (RelativeLayout.LayoutParams) mStatusText .getLayoutParams(); statusParams.setMargins(FormattingUtils.dpToPx(this, 48), 0, 0, 0); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { statusParams.setMarginEnd(0); } statusParams.addRule(RelativeLayout.LEFT_OF, R.id.view_key_qr_code_layout); mStatusText.setLayoutParams(statusParams); mActionEncryptFile.setVisibility(View.VISIBLE); mActionEncryptText.setVisibility(View.VISIBLE); showFab(); // noinspection deprecation (no getDrawable with theme at current minApi level 15!) mFab.setImageDrawable(getResources().getDrawable(R.drawable.ic_repeat_white_24dp)); } else { mActionEncryptFile.setVisibility(View.VISIBLE); mActionEncryptText.setVisibility(View.VISIBLE); mQrCodeLayout.setVisibility(View.GONE); if (mIsVerified) { mStatusText.setText(R.string.view_key_verified); mStatusImage.setVisibility(View.VISIBLE); KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText, State.VERIFIED, R.color.icons, true); // noinspection deprecation, fix requires api level 23 color = getResources().getColor(R.color.key_flag_green); photoTask.execute(mMasterKeyId); hideFab(); } else { mStatusText.setText(R.string.view_key_unverified); mStatusImage.setVisibility(View.VISIBLE); KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText, State.UNVERIFIED, R.color.icons, true); // noinspection deprecation, fix requires api level 23 color = getResources().getColor(R.color.key_flag_orange); showFab(); } } if (mPreviousColor == 0 || mPreviousColor == color) { mAppBarLayout.setBackgroundColor(color); mCollapsingToolbarLayout.setContentScrimColor(color); mCollapsingToolbarLayout.setStatusBarScrimColor(getStatusBarBackgroundColor(color)); mPreviousColor = color; } else { ObjectAnimator colorFade = ObjectAnimator.ofObject(mAppBarLayout, "backgroundColor", new ArgbEvaluator(), mPreviousColor, color); mCollapsingToolbarLayout.setContentScrimColor(color); mCollapsingToolbarLayout.setStatusBarScrimColor(getStatusBarBackgroundColor(color)); colorFade.setDuration(1200); colorFade.start(); mPreviousColor = color; } //noinspection deprecation mStatusImage.setAlpha(80); break; } } } }
From source file:at.bitfire.davdroid.mirakel.resource.LocalAddressBook.java
protected void populatePhoto(Contact c) throws RemoteException { @Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Photo.PHOTO_FILE_ID, Photo.PHOTO }, Photo.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?", new String[] { String.valueOf(c.getLocalID()), Photo.CONTENT_ITEM_TYPE }, null); if (cursor != null && cursor.moveToNext()) { if (!cursor.isNull(0)) { Uri photoUri = Uri.withAppendedPath( ContentUris.withAppendedId(RawContacts.CONTENT_URI, c.getLocalID()), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try { @Cleanup//from w w w . j ava 2 s . c o m AssetFileDescriptor fd = providerClient.openAssetFile(photoUri, "r"); @Cleanup InputStream is = fd.createInputStream(); c.setPhoto(IOUtils.toByteArray(is)); } catch (IOException ex) { Log.w(TAG, "Couldn't read high-res contact photo", ex); } } else c.setPhoto(cursor.getBlob(1)); } }
From source file:mobisocial.socialkit.musubi.Musubi.java
public DbObj objForCursor(Cursor cursor) { try {//www .j av a 2 s . co m long localId = -1; String appId = null; String type = null; String name = null; JSONObject json = null; long senderId = -1; byte[] hash = null; long feedId = -1; Integer intKey = null; long timestamp = -1; Long parentId = null; try { localId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_ID)); } catch (IllegalArgumentException e) { } try { appId = cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_APP_ID)); } catch (IllegalArgumentException e) { } try { type = cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_TYPE)); } catch (IllegalArgumentException e) { } try { name = cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_STRING_KEY)); } catch (IllegalArgumentException e) { } try { json = new JSONObject(cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_JSON))); } catch (IllegalArgumentException e) { } try { senderId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_IDENTITY_ID)); } catch (IllegalArgumentException e) { } try { hash = cursor.getBlob(cursor.getColumnIndexOrThrow(DbObj.COL_UNIVERSAL_HASH)); } catch (IllegalArgumentException e) { } try { feedId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_FEED_ID)); } catch (IllegalArgumentException e) { } try { intKey = cursor.getInt(cursor.getColumnIndexOrThrow(DbObj.COL_INT_KEY)); } catch (IllegalArgumentException e) { } try { timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_TIMESTAMP)); } catch (IllegalArgumentException e) { } try { int pIndex = cursor.getColumnIndexOrThrow(DbObj.COL_PARENT_ID); if (!cursor.isNull(pIndex)) { parentId = cursor.getLong(pIndex); } } catch (IllegalArgumentException e) { } // Don't require raw field. final byte[] raw; int rawIndex = cursor.getColumnIndex(DbObj.COL_RAW); if (rawIndex == -1 || cursor.isNull(rawIndex)) { raw = null; } else { raw = cursor.getBlob(cursor.getColumnIndexOrThrow(DbObj.COL_RAW)); } return new DbObj(this, appId, feedId, parentId, senderId, localId, type, json, raw, intKey, name, timestamp, hash); } catch (JSONException e) { Log.e(TAG, "Couldn't parse obj.", e); return null; } }