Example usage for android.database Cursor getColumnIndexOrThrow

List of usage examples for android.database Cursor getColumnIndexOrThrow

Introduction

In this page you can find the example usage for android.database Cursor getColumnIndexOrThrow.

Prototype

int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;

Source Link

Document

Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist.

Usage

From source file:com.zhihuigu.sosoOffice.RegisterThirdActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) { //  RESULT_OK 
        Log.e(TAG, "ActivityResult resultCode error");
        return;/*from w  w  w.  j a va  2s.  c o m*/
    }
    Bitmap bm = null;
    // ContentProvider ContentResolver
    ContentResolver resolver = getContentResolver();
    // Activity
    if (requestCode == IMAGE_CODE) {//
        String imgPath = "";
        Uri originalUri = data.getData(); // uri
        //            bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); // bitmap
        // 
        String[] proj = { MediaStore.Images.Media.DATA };
        // androidAndroid
        Cursor cursor = managedQuery(originalUri, proj, null, null, null);
        //            InputStream inputStream = resolver.openInputStream(originalUri);

        //  
        if (cursor == null) {
            imgPath = data.getDataString();
        } else {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            //  
            cursor.moveToFirst();
            // www.2cto.com
            imgPath = cursor.getString(column_index);
        }
        File fromFile = new File(imgPath);
        // 
        if (fromFile != null && fromFile.exists()) {
            fileName = CommonUtils.getFileName();// 
            File file = new File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD);
            if (!file.exists()) {
                file.mkdirs();
            }
            sdcardTempFile = new File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD, fileName);
            //               path = sdcardTempFile.getAbsolutePath();
            //               FileCopyUtil.copyfile(fromFile, sdcardTempFile, true);

            Bitmap bitmap = CommonUtils.getDrawable(fromFile.getAbsolutePath(),
                    MyApplication.getInstance(this).getScreenWidth(),
                    MyApplication.getInstance(this).getScreenHeight());
            if (bitmap != null) {
                bitmap = CommonUtils.RotateImg(fromFile.getAbsolutePath(), bitmap);//90
                boolean copy = CommonUtils.OutPutImage(sdcardTempFile, bitmap);
                if (copy) {
                    /*File oldFile = new File(path);
                    oldFile.delete();*/
                    path = sdcardTempFile.getAbsolutePath();
                }
            } else {
                Toast.makeText(this, "", 3000).show();
            }
        }
        bm = CommonUtils.getDrawable(path, photoView);
        if (bm != null) {
            photo_tag = true;
            bottomLinear.setVisibility(View.GONE);
            photoView.setImageBitmap(bm);
        } else {
            Toast.makeText(this, "", 3000).show();
        }

    } else if (requestCode == REQUEST_CODE_TAKE_PICTURE) {//   
        fileName = CommonUtils.getFileName();// 
        File file = new File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD);
        if (!file.exists()) {
            file.mkdirs();
        }
        sdcardTempFile = new File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD, fileName);
        if (CommonUtils.isHasSdcard()) {//   
            path = SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD + MyApplication.getInstance(this).getFileName();
            Bitmap bitmap = CommonUtils.getDrawable(path, 500, 500);
            bitmap = CommonUtils.RotateImg(path, bitmap);//90
            boolean copy = CommonUtils.OutPutImage(sdcardTempFile, bitmap);
            if (copy) {
                File oldFile = new File(path);
                oldFile.delete();
                path = sdcardTempFile.getAbsolutePath();
            }

            bm = CommonUtils.getDrawable(path, photoView);
            photo_tag = true;
            bottomLinear.setVisibility(View.GONE);
            photoView.setImageBitmap(bm);
        } else { //    
            Bundle extras = data.getExtras();
            bm = (Bitmap) extras.get("data");
            photo_tag = true;
            bottomLinear.setVisibility(View.GONE);
            photoView.setImageBitmap(bm);
        }
    }
}

From source file:com.chuxin.family.widgets.CxImagePager.java

private String getFilePathByUri(String uri) {
    if (null == uri) {
        return null;
    }//from w  ww. j  a  va  2 s.c  o  m
    Uri path = Uri.parse(uri);
    Cursor c = getContentResolver().query(path, null, null, null, null);
    String filePath = null;
    if (null == c) {
        return null;
    }
    try {
        if ((c.getCount() != 1) || !c.moveToFirst()) {
            // nothing to do
        } else {
            filePath = c.getString(c.getColumnIndexOrThrow(MediaColumns.DATA));
        }
    } finally {
        c.close();
    }
    return filePath;
}

From source file:net.kourlas.voipms_sms.Database.java

/**
 * Gets the message with the specified VoIP.ms ID from the database.
 *
 * @return The message with the specified VoIP.ms ID.
 *//*from   www .  j a v  a2  s . c o m*/
public synchronized Message getMessageWithVoipId(String did, long voipId) {
    Cursor cursor = database.query(TABLE_MESSAGE, columns,
            COLUMN_DID + "=" + did + " AND " + COLUMN_VOIP_ID + " = " + voipId, null, null, null, null);
    if (cursor.moveToFirst()) {
        Message message = new Message(cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATABASE_ID)),
                cursor.isNull(cursor.getColumnIndexOrThrow(COLUMN_VOIP_ID)) ? null
                        : cursor.getLong(cursor.getColumnIndex(COLUMN_VOIP_ID)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TYPE)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DID)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MESSAGE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_UNREAD)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELETED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERY_IN_PROGRESS)));
        cursor.close();
        return message;
    } else {
        cursor.close();
        return null;
    }
}

From source file:net.kourlas.voipms_sms.Database.java

/**
 * Gets the message with the specified database ID from the database.
 *
 * @return The message with the specified database ID.
 *///from  w w w .  j  av  a2 s.  c o m
public synchronized Message getMessageWithDatabaseId(String did, long databaseId) {
    Cursor cursor = database.query(TABLE_MESSAGE, columns,
            COLUMN_DID + "=" + did + " AND " + COLUMN_DATABASE_ID + " = " + databaseId, null, null, null, null);
    if (cursor.moveToFirst()) {
        Message message = new Message(cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATABASE_ID)),
                cursor.isNull(cursor.getColumnIndexOrThrow(COLUMN_VOIP_ID)) ? null
                        : cursor.getLong(cursor.getColumnIndex(COLUMN_VOIP_ID)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TYPE)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DID)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MESSAGE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_UNREAD)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELETED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERY_IN_PROGRESS)));
        cursor.close();
        return message;
    } else {
        cursor.close();
        return null;
    }
}

From source file:com.ezac.gliderlogs.FlightDetailActivity.java

private void fillData(Uri uri) {
    String[] projection = { GliderLogTables.F_DATE, GliderLogTables.F_REGISTRATION, GliderLogTables.F_CALLSIGN,
            GliderLogTables.F_TYPE, GliderLogTables.F_INSTRUCTION, GliderLogTables.F_PILOT,
            GliderLogTables.F_COPILOT, GliderLogTables.F_STARTED, GliderLogTables.F_LANDED,
            GliderLogTables.F_DURATION, GliderLogTables.F_LAUNCH, GliderLogTables.F_NOTES };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();/*from w w w.j a  va  2 s  .c  o m*/
        mDateText.setText(cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_DATE)));
        String sp_regi = cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_REGISTRATION));
        mCallSign = cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_CALLSIGN));
        for (int i = 0; i < mRegiSpin.getCount(); i++) {
            String s = (String) mRegiSpin.getItemAtPosition(i);
            if (s.equalsIgnoreCase(sp_regi)) {
                mRegiSpin.setSelection(i);
            }
        }

        // get record value for type and set controls
        String sp_type = cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_TYPE));
        mTypeNorm.setChecked(false);
        mTypePass.setChecked(false);
        mTypeDona.setChecked(false);
        mTypeClub.setChecked(false);
        if (sp_type.equals("")) {
            mTypeNorm.setChecked(true);
        }
        if (sp_type.equals("PASS")) {
            mTypePass.setChecked(true);
        }
        if (sp_type.equals("DONA")) {
            mTypeDona.setChecked(true);
        }
        if (sp_type.equals("CLUB")) {
            mTypeClub.setChecked(true);
        }

        // get record value for pilot and set spinner
        String sp_pilo = cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_PILOT));
        for (int i = 0; i < mPilotSpin.getCount(); i++) {
            String s = (String) mPilotSpin.getItemAtPosition(i);
            if (s.equalsIgnoreCase(sp_pilo)) {
                mPilotSpin.setSelection(i);
            }
        }
        // get record value for copilot and set spinner
        String sp_copi = cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_COPILOT));
        for (int i = 0; i < mCoPilotSpin.getCount(); i++) {
            String s = (String) mCoPilotSpin.getItemAtPosition(i);
            if (s.equalsIgnoreCase(sp_copi)) {
                mCoPilotSpin.setSelection(i);
            }
        }
        if (cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_INSTRUCTION)).equals("N")) {
            mInstChck.setChecked(false);
        }
        if (cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_INSTRUCTION)).equals("J")) {
            mInstChck.setChecked(true);
        }
        mStartText.setText(cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_STARTED)));
        mLandText.setText(cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_LANDED)));
        mDuraText.setText(cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_DURATION)));
        String sp_laun = cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_LAUNCH));
        mLaunchWinch.setChecked(false);
        mLaunchTow.setChecked(false);
        mLaunchMotor.setChecked(false);
        if (sp_laun.equals("L")) {
            mLaunchWinch.setChecked(true);
        }
        if (sp_laun.equals("S")) {
            mLaunchTow.setChecked(true);
        }
        if (sp_laun.equals("M")) {
            mLaunchMotor.setChecked(true);
        }
        mBodyText.setText(cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_NOTES)));
        // always close the cursor
        cursor.close();
    }
}

From source file:com.gsma.rcs.ri.messaging.GroupTalkView.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_gchat_item, menu);
    menu.findItem(R.id.menu_display_content).setVisible(false);
    menu.findItem(R.id.menu_listen_content).setVisible(false);
    /* Get the list item position. */
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    Cursor cursor = (Cursor) mAdapter.getItem(info.position);
    int providerId = cursor.getInt(cursor.getColumnIndexOrThrow(HistoryLog.PROVIDER_ID));
    Direction direction = Direction.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(Message.DIRECTION)));
    if (FileTransferLog.HISTORYLOG_MEMBER_ID == providerId) {
        String mimeType = cursor.getString(cursor.getColumnIndexOrThrow(HistoryLog.MIME_TYPE));
        if (Utils.isImageType(mimeType)) {
            if (Direction.OUTGOING == direction) {
                menu.findItem(R.id.menu_display_content).setVisible(true);

            } else {
                Long transferred = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.TRANSFERRED));
                Long size = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.FILESIZE));
                if (size.equals(transferred)) {
                    menu.findItem(R.id.menu_display_content).setVisible(true);
                }/*ww w  .  j  a va2 s  . c  o m*/
            }
        } else if (Utils.isAudioType(mimeType)) {
            if (Direction.OUTGOING == direction) {
                menu.findItem(R.id.menu_listen_content).setVisible(true);

            } else {
                Long transferred = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.TRANSFERRED));
                Long size = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.FILESIZE));
                if (size.equals(transferred)) {
                    menu.findItem(R.id.menu_listen_content).setVisible(true);
                }
            }
        }
    }
    if (Direction.OUTGOING != direction) {
        menu.findItem(R.id.menu_view_group_delivery).setVisible(false);
    }
}

From source file:br.com.bioscada.apps.biotracks.TrackListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (BuildConfig.DEBUG) {
        ApiAdapterFactory.getApiAdapter().enableStrictMode();
    }// w w w .  ja  va 2 s  .co m

    myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
    sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);

    trackRecordingServiceConnection = new TrackRecordingServiceConnection(this, bindChangedCallback);
    trackController = new TrackController(this, trackRecordingServiceConnection, true, recordListener,
            stopListener);

    setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
    // Show trackController when search dialog is dismissed
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchManager.setOnDismissListener(new SearchManager.OnDismissListener() {
        @Override
        public void onDismiss() {
            trackController.show();
        }
    });

    listView = (ListView) findViewById(R.id.track_list);
    listView.setEmptyView(findViewById(R.id.track_list_empty_view));
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent newIntent = IntentUtils.newIntent(TrackListActivity.this, TrackDetailActivity.class)
                    .putExtra(TrackDetailActivity.EXTRA_TRACK_ID, id);
            startActivity(newIntent);
        }
    });
    sectionResourceCursorAdapter = new SectionResourceCursorAdapter(this, R.layout.list_item, null, 0) {
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            int idIndex = cursor.getColumnIndex(TracksColumns._ID);
            int iconIndex = cursor.getColumnIndex(TracksColumns.ICON);
            int nameIndex = cursor.getColumnIndex(TracksColumns.NAME);
            int sharedOwnerIndex = cursor.getColumnIndex(TracksColumns.SHAREDOWNER);
            int totalTimeIndex = cursor.getColumnIndexOrThrow(TracksColumns.TOTALTIME);
            int totalDistanceIndex = cursor.getColumnIndexOrThrow(TracksColumns.TOTALDISTANCE);
            int startTimeIndex = cursor.getColumnIndexOrThrow(TracksColumns.STARTTIME);
            int categoryIndex = cursor.getColumnIndex(TracksColumns.CATEGORY);
            int descriptionIndex = cursor.getColumnIndex(TracksColumns.DESCRIPTION);

            long trackId = cursor.getLong(idIndex);
            boolean isRecording = trackId == recordingTrackId;
            String icon = cursor.getString(iconIndex);
            int iconId = TrackIconUtils.getIconDrawable(icon);
            String name = cursor.getString(nameIndex);
            String sharedOwner = cursor.getString(sharedOwnerIndex);
            String totalTime = StringUtils.formatElapsedTime(cursor.getLong(totalTimeIndex));
            String totalDistance = StringUtils.formatDistance(TrackListActivity.this,
                    cursor.getDouble(totalDistanceIndex), metricUnits);
            int markerCount = 0;
            try {
                markerCount = myTracksProviderUtils.getWaypointCount(trackId);
            } catch (Exception e) {
                Log.d("BIOTRACKS", e.getMessage());
            }

            long startTime = cursor.getLong(startTimeIndex);
            String category = icon != null && !icon.equals("") ? null : cursor.getString(categoryIndex);
            String description = cursor.getString(descriptionIndex);

            ListItemUtils.setListItem(TrackListActivity.this, view, isRecording, recordingTrackPaused, iconId,
                    R.string.image_track, name, sharedOwner, totalTime, totalDistance, markerCount, startTime,
                    true, category, description, null);
        }
    };
    listView.setAdapter(sectionResourceCursorAdapter);
    ApiAdapterFactory.getApiAdapter().configureListViewContextualMenu(this, listView,
            contextualActionModeCallback);

    getSupportLoaderManager().initLoader(0, null, loaderCallbacks);
    showStartupDialogs();
}

From source file:com.gsma.rcs.ri.messaging.GroupTalkView.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    Cursor cursor = (Cursor) (mAdapter.getItem(info.position));
    int providerId = cursor.getInt(cursor.getColumnIndexOrThrow(HistoryLog.PROVIDER_ID));
    String id = cursor.getString(cursor.getColumnIndexOrThrow(HistoryLog.ID));
    if (LogUtils.isActive) {
        Log.d(LOGTAG, "onContextItemSelected Id=".concat(id));
    }/*from  w  w w. ja  va2s .  com*/
    try {
        switch (item.getItemId()) {
        case R.id.menu_view_group_delivery:
            GroupDeliveryInfoList.startActivity(this, id);
            return true;

        case R.id.menu_delete_message:
            if (ChatLog.Message.HISTORYLOG_MEMBER_ID == providerId) {
                mChatService.deleteMessage(id);
            } else {
                mFileTransferService.deleteFileTransfer(id);
            }
            return true;

        case R.id.menu_view_detail:
            if (ChatLog.Message.HISTORYLOG_MEMBER_ID == providerId) {
                ChatMessageLogView.startActivity(this, id);
            } else {
                FileTransferLogView.startActivity(this, id);
            }
            return true;

        case R.id.menu_display_content:
            if (FileTransferLog.HISTORYLOG_MEMBER_ID == providerId) {
                String file = cursor.getString(cursor.getColumnIndexOrThrow(HistoryLog.CONTENT));
                Utils.showPicture(this, Uri.parse(file));
                markFileTransferAsRead(cursor, id);
                return true;
            }
            break;

        case R.id.menu_listen_content:
            if (FileTransferLog.HISTORYLOG_MEMBER_ID == providerId) {
                String file = cursor.getString(cursor.getColumnIndexOrThrow(HistoryLog.CONTENT));
                Utils.playAudio(this, Uri.parse(file));
                markFileTransferAsRead(cursor, id);
                return true;
            }
            break;
        }
        return super.onContextItemSelected(item);
    } catch (RcsServiceException e) {
        showException(e);
    }
    return true;
}

From source file:com.netcompss.ffmpeg4android_client.BaseWizard.java

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();//from ww  w .j a  va 2s. c  om
    return cursor.getString(column_index);
}

From source file:net.kourlas.voipms_sms.Database.java

/**
 * Gets all of the messages in the database except for deleted messages.
 *
 * @return All of the messages in the database except for deleted messages.
 *///from   w  w w  .  j  a va 2  s. c o  m
public synchronized Message[] getUndeletedMessages(String did) {
    List<Message> messages = new ArrayList<>();

    Cursor cursor = database.query(TABLE_MESSAGE, columns,
            COLUMN_DID + "=" + did + " AND " + COLUMN_DELETED + "=" + "0", null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Message message = new Message(cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATABASE_ID)),
                cursor.isNull(cursor.getColumnIndexOrThrow(COLUMN_VOIP_ID)) ? null
                        : cursor.getLong(cursor.getColumnIndex(COLUMN_VOIP_ID)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TYPE)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DID)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MESSAGE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_UNREAD)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELETED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERY_IN_PROGRESS)));
        messages.add(message);
        cursor.moveToNext();
    }
    cursor.close();

    Collections.sort(messages);

    Message[] messageArray = new Message[messages.size()];
    return messages.toArray(messageArray);
}