Example usage for android.database Cursor getString

List of usage examples for android.database Cursor getString

Introduction

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

Prototype

String getString(int columnIndex);

Source Link

Document

Returns the value of the requested column as a String.

Usage

From source file:com.amytech.android.library.views.imagechooser.threads.MediaProcessorThread.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {/*from  ww  w.j  a  v  a  2s.com*/
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:com.dongfang.dicos.sina.UtilSina.java

/**
 * Get a HttpClient object which is setting correctly .
 * // www  .  ja  va2s .  c  o  m
 * @param context
 *            : context of activity
 * @return HttpClient: HttpClient object
 */
public static HttpClient getHttpClient(Context context) {
    BasicHttpParams httpParameters = new BasicHttpParams();
    // Set the default socket timeout (SO_TIMEOUT) // in
    // milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setConnectionTimeout(httpParameters, UtilSina.SET_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, UtilSina.SET_SOCKET_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParameters);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        // ??APN
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            // ???
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                HttpHost proxy = new HttpHost(proxyStr, 80);
                client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
            }
            mCursor.close();
        }
    }
    return client;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

/**
 * For a given file {@link Uri} string, we check if its hash has a
 * corresponding entry in the {@link MetaProvider}, telling thus whether the
 * file from under given {@link Uri} string has been already uploaded.
 * /* w w  w . j  ava  2s  .  c o m*/
 * @param uriString
 *            the uri string which content we are checking
 * @return resourcePath if content under uri has been already uploaded,
 *         null otherwise
 */
public static String isUploaded(String uriString) {
    File file = null;
    String fileHash = null;

    if (uriString.startsWith(ContentResolver.SCHEME_CONTENT)) {
        final String[] projection = new String[] { MediaColumns.DATA };
        final Cursor c = sResolver.query(Uri.parse(uriString), projection, null, null, null);
        try {
            if (c.moveToFirst()) {
                String data = c.getString(c.getColumnIndex(MediaColumns.DATA));
                file = new File(data);
            } else {
                return null;
            }
        } finally {
            c.close();
        }
    } else if (uriString.startsWith(ContentResolver.SCHEME_FILE)) {
        final URI fileURI = URI.create(Uri.encode(uriString, ":/"));
        file = new File(fileURI);
    } else {
        Log.e(TAG, "Tried to check malformed uri string: " + uriString);
        return null;
    }

    try {
        if (file != null && file.exists()) {
            fileHash = HashUtils.getSha1(file);
            Log.d(TAG, String.format("Computed hash: '%s'", fileHash));
        } else {
            throw new FileNotFoundException("isUploaded()");
        }
    } catch (Exception e) {
        Log.e(TAG, "Can't compute file hash!", e);
        return null;
    }

    final String[] projection = new String[] { Nodes.NODE_RESOURCE_PATH };
    final String selection = Nodes.NODE_HASH + "=?";
    final String[] selectionArgs = new String[] { fileHash };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    String resourcePath = null;
    try {
        if (c.moveToFirst()) {
            resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
            Log.d(TAG, "Corresponding file hash found: " + resourcePath);
        } else {
            Log.d(TAG, "Corresponding file hash not found.");
        }
    } finally {
        c.close();
    }
    return resourcePath;
}

From source file:curso.android.DAO.RespostaDAO.java

public static List<Resposta> readAll() {
    Cursor cursor = null;
    try {/*from ww  w  . j a  v a2  s.  c  o  m*/
        List<Resposta> all = new ArrayList<Resposta>();

        cursor = Const.db.rawQuery("SELECT * FROM resposta", null);

        if (cursor.getCount() > 0) {
            int idIndex = cursor.getColumnIndex("asw_id");
            int askIndex = cursor.getColumnIndex("ask_id");
            int userIndex = cursor.getColumnIndex("user_id");
            int respostaIndex = cursor.getColumnIndex("asw_resposta");
            int nameIndex = cursor.getColumnIndex("user_name");
            cursor.moveToFirst();
            do {
                Long id = Long.valueOf(cursor.getInt(idIndex));
                Long ask = Long.valueOf(cursor.getString(askIndex));
                Long user = Long.valueOf(cursor.getString(userIndex));
                String pergunta = cursor.getString(respostaIndex);
                String user_name = cursor.getString(nameIndex);

                Resposta asw = new Resposta();
                asw.setAsw_id(id);
                asw.setAsk_id(ask);
                asw.setUser_id(user);
                asw.setAsw_resposta(pergunta);
                asw.setUser_name(user_name);

                all.add(asw);

                cursor.moveToNext();
            } while (!cursor.isAfterLast());
        }

        return all;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.zzl.zl_app.cache.Utility.java

/**
 * Get a HttpClient object which is setting correctly .
 * /*from w w w. j  a  v  a 2s . c o m*/
 * @param context
 *            : context of activity
 * @return HttpClient: HttpClient object
 */
public static HttpClient getHttpClient(Context context) {
    BasicHttpParams httpParameters = new BasicHttpParams();
    // Set the default socket timeout (SO_TIMEOUT) // in
    // milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setConnectionTimeout(httpParameters, Utility.SET_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, Utility.SET_SOCKET_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParameters);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
    if (!wifiManager.isWifiEnabled() || -1 == info.getNetworkId()) {
        // ??APN?
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            // ???
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                HttpHost proxy = new HttpHost(proxyStr, 80);
                client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
            }
            mCursor.close();
        }
    }
    return client;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static Set<String> getUserNodePaths() {
    Set<String> userNodePaths = new TreeSet<String>();
    final String[] projection = new String[] { Nodes._ID, Nodes.NODE_RESOURCE_PATH };
    final String selection = Nodes.NODE_PARENT_PATH + " IS NULL";
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, null, null);
    try {/*from w  w w.j  ava 2s  .  c  o  m*/
        if (c.moveToFirst()) {
            String resourcePath;
            do {
                resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
                userNodePaths.add(resourcePath);
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return userNodePaths;
}

From source file:com.dongfang.dicos.sina.UtilSina.java

public static HttpClient getNewHttpClient(Context context) {
    try {/*from w w w  . j  av a  2 s  .  c  o  m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 10000);

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        // Set the default socket timeout (SO_TIMEOUT) // in
        // milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setConnectionTimeout(params, UtilSina.SET_CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, UtilSina.SET_SOCKET_TIMEOUT);
        HttpClient client = new DefaultHttpClient(ccm, params);
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            // ??APN
            Uri uri = Uri.parse("content://telephony/carriers/preferapn");
            Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                // ???
                String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
                if (proxyStr != null && proxyStr.trim().length() > 0) {
                    HttpHost proxy = new HttpHost(proxyStr, 80);
                    client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
                }
                mCursor.close();
            }
        }
        return client;
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:curso.android.DAO.RespostaDAO.java

public static List<Resposta> getRespostaPergunta(Pergunta p) {
    Cursor cursor = null;
    try {//w  ww  .  j a  v a2 s.  c  o m
        List<Resposta> questions = new ArrayList<Resposta>();

        cursor = Const.db.rawQuery("SELECT * FROM resposta WHERE ask_id = " + p.getAsk_id(), null);

        if (cursor.getCount() > 0) {
            int idIndex = cursor.getColumnIndex("asw_id");
            int askIndex = cursor.getColumnIndex("ask_id");
            int userIndex = cursor.getColumnIndex("user_id");
            int respostaIndex = cursor.getColumnIndex("asw_resposta");
            int nameIndex = cursor.getColumnIndex("user_name");
            cursor.moveToFirst();
            do {
                Long id = Long.valueOf(cursor.getInt(idIndex));
                Long ask = Long.valueOf(cursor.getString(askIndex));
                Long user = Long.valueOf(cursor.getInt(userIndex));
                String resposta = cursor.getString(respostaIndex);
                String user_name = cursor.getString(nameIndex);

                Resposta answer = new Resposta();
                answer.setAsw_id(id);
                answer.setAsk_id(ask);
                answer.setUser_id(user);
                answer.setAsw_resposta(resposta);
                answer.setUser_name(user_name);

                questions.add(answer);

                cursor.moveToNext();
            } while (!cursor.isAfterLast());
        }

        return questions;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.zzl.zl_app.cache.Utility.java

public static HttpClient getNewHttpClient(Context context) {
    try {//from www .  j a v  a 2 s.  c o m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        // Set the default socket timeout (SO_TIMEOUT) // in
        // milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setConnectionTimeout(params, Utility.SET_CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, Utility.SET_SOCKET_TIMEOUT);
        HttpClient client = new DefaultHttpClient(ccm, params);

        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiManager.getConnectionInfo();
        if (!wifiManager.isWifiEnabled() || -1 == info.getNetworkId()) {
            // ??APN?
            Uri uri = Uri.parse("content://telephony/carriers/preferapn");
            Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                // ???
                String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
                if (proxyStr != null && proxyStr.trim().length() > 0) {
                    HttpHost proxy = new HttpHost(proxyStr, 80);
                    client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
                }
                mCursor.close();
            }
        }
        return client;
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static String uriToPath(Context activity, Uri uri) {
    if (null == uri) {
        return null;
    }// ww  w .ja  v a2s  . c o  m
    String urlStr = uri.toString();
    if (urlStr.startsWith("file://")) {
        return uri.getPath();
    }
    Cursor cursor = null;
    String idWhere;
    String id;
    String[] columns = { MediaStore.Images.Media.DATA };
    try {
        if (Build.VERSION.SDK_INT == 19 && DocumentsContract.isDocumentUri(activity, uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            id = split[1];
            idWhere = MediaStore.Images.Media._ID + "=?";
            cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                    idWhere, new String[] { id }, null);
        } else {
            cursor = activity.getContentResolver().query(uri, columns, null, null, null);
        }
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        }
    } catch (Exception e) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}