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:cn.kangeqiu.kq.activity.ChatActivity.java

/**
 * ??// ww w .j a v a  2  s  .  c  o  m
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, 0).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?chattype,??
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}

From source file:org.cryptsecure.Utility.java

/**
 * Convert the image URI to the direct file system path of the image so that
 * it can be loaded.//  ww  w  .j a  v  a2s .  c om
 * 
 * @param contentUri
 *            the content uri
 * @return the real path from uri
 */
@SuppressLint("NewApi")
// file
@SuppressWarnings("deprecation")
public static String getRealPathFromURI(Activity activity, Uri contentUri) {
    String returnPath = null;

    Log.d("communicator", "IMPORT getRealPathFromURI() contentUri=" + contentUri.toString());

    try {
        if (Build.VERSION.SDK_INT < 19) {
            // can post image
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = activity.managedQuery(contentUri, proj, // Which
                    // columns
                    // to
                    // return
                    null, // WHERE clause; which rows to return (all rows)
                    null, // WHERE clause selection arguments (none)
                    null); // Order-by clause (ascending by name)
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            returnPath = cursor.getString(column_index);
        } else if (Build.VERSION.SDK_INT > 19) {
            String[] projection = { MediaStore.Images.Media.DATA };
            String wholeID = DocumentsContract.getDocumentId(contentUri);
            String id = wholeID.split(":")[1];
            String sel = MediaStore.Images.Media._ID + "=?";
            Cursor cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, sel, new String[] { id }, null);
            int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            returnPath = cursor.getString(column_index).toString();
            cursor.close();
        } else {
            returnPath = contentUri.toString();
        }
    } catch (Exception e) {
    }

    Log.d("communicator", "IMPORT getRealPathFromURI() returnPath=" + returnPath);

    return returnPath;
}

From source file:com.example.Bama.chat.chatuidemo.activity.ChatActivity.java

/**
 * ??/*from  ww  w.j  a  va2 s .  c  o  m*/
 *
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, Toast.LENGTH_SHORT).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, Toast.LENGTH_SHORT).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?chattype,??
    if (chatType == CHATTYPE_GROUP) {
        message.setChatType(ChatType.GroupChat);
    } else if (chatType == CHATTYPE_CHATROOM) {
        message.setChatType(ChatType.ChatRoom);
    }

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refreshSelectLast();
    setResult(RESULT_OK);
}

From source file:cn.ucai.yizhesale.activity.ChatActivity.java

/**
 * ??/*from w w  w . java 2s .c  o  m*/
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        String st7 = getResources().getString(cn.ucai.yizhesale.R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, Toast.LENGTH_SHORT).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(cn.ucai.yizhesale.R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, Toast.LENGTH_SHORT).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?chattype,??
    if (chatType == CHATTYPE_GROUP) {
        message.setChatType(ChatType.GroupChat);
    } else if (chatType == CHATTYPE_CHATROOM) {
        message.setChatType(ChatType.ChatRoom);
    }

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);
    if (isRobot) {
        message.setAttribute("em_robot_message", true);
    }
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refreshSelectLast();
    setResult(RESULT_OK);
}

From source file:me.piebridge.bible.Bible.java

private String getVersionMetadata(String name, SQLiteDatabase metadata, String defaultValue) {
    String value = defaultValue;// w ww  . j  a v  a2 s.c  o m
    Cursor cursor = metadata.query("metadata", new String[] { "value" }, "name = ? or name = ?",
            new String[] { name, name + "_" + Locale.getDefault().toString() }, null, null, "name desc", "1");
    while (cursor != null && cursor.moveToNext()) {
        value = cursor.getString(cursor.getColumnIndexOrThrow("value"));
        break;
    }
    if (cursor != null) {
        cursor.close();
    }
    return value;
}

From source file:com.lewa.crazychapter11.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    switch (requestCode) {
    case PICK_CONTACT:
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = intent.getData();
            CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null);

            Cursor cursor = cursorLoader.loadInBackground();
            if (cursor != null && cursor.moveToFirst()) {
                Log.i("algerheContact", " comhere 111");
                String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cursor
                        .getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                String phoneNumber = "????";

                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
                if (phones == null) {
                    return;
                }//w  ww .j av  a  2  s. com
                // Log.i("algerheContact"," comhere 222 phones.moveToFirst()="+phones.moveToFirst());
                if (phones.moveToFirst()) {
                    phoneNumber = phones
                            .getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                phones.close();
                EditText show = (EditText) findViewById(R.id.show);

                show.setText(name);
                EditText phone = (EditText) findViewById(R.id.phone);
                phone.setText(phoneNumber);

                show.setVisibility(View.GONE);
                phone.setVisibility(View.GONE);
            }
            cursor.close();
        }
        break;
    }

    if (requestCode == 0 && resultCode == 0 && intent != null) {
        Bundle data = intent.getExtras();
        if (data == null) {
            return;
        }
        String resultArms = data.getString("armType");
        String showArms = "?" + resultArms;
        show_txt = (EditText) findViewById(R.id.show_txt);
        if (resultArms != null && show_txt != null) {
            show_txt.setText(showArms);
        }
    }
}

From source file:com.dcy.psychology.ChatActivity.java

/**
 * ?// ww w  . ja va  2s .  c  o m
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, 0).show();
        return;
    }

    // ??
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?????chattype,?
    if (chatType == CHATTYPE_GROUP) {
        message.setChatType(ChatType.GroupChat);
    } else if (chatType == CHATTYPE_CHATROOM) {
        message.setChatType(ChatType.ChatRoom);
    }

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);
    if (isRobot) {
        message.setAttribute("em_robot_message", true);
    }
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refreshSelectLast();
    setResult(RESULT_OK);
}

From source file:cmu.cconfs.instantMessage.activities.ChatActivity.java

/**
 * ??// ww w.  ja  va 2 s.  co  m
 *
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, 0).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?chattype,??
    if (chatType == CHATTYPE_GROUP) {
        message.setChatType(ChatType.GroupChat);
    } else if (chatType == CHATTYPE_CHATROOM) {
        message.setChatType(ChatType.ChatRoom);
    }

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);
    if (isRobot) {
        message.setAttribute("em_robot_message", true);
    }
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refreshSelectLast();
    setResult(RESULT_OK);
}

From source file:com.example.chudong.telescope.activity.ChatActivity.java

/**
 * ??//from  w w  w .j av a 2 s.c  om
 *
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, Toast.LENGTH_SHORT).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, 0).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?chattype,??
    if (chatType == CHATTYPE_GROUP) {
        message.setChatType(ChatType.GroupChat);
    } else if (chatType == CHATTYPE_CHATROOM) {
        message.setChatType(ChatType.ChatRoom);
    }

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);
    if (isRobot) {
        message.setAttribute("em_robot_message", true);
    }
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refreshSelectLast();
    setResult(RESULT_OK);
}

From source file:com.grass.caishi.cc.activity.ChatActivity.java

/**
 * ??/*from   ww  w .ja  v  a  2s . co  m*/
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        Toast.makeText(getApplicationContext(), "?", 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        Toast.makeText(getApplicationContext(), "?10M", 0).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?chattype,??
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);

    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}