Example usage for android.content ContentResolver query

List of usage examples for android.content ContentResolver query

Introduction

In this page you can find the example usage for android.content ContentResolver query.

Prototype

public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection,
        @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder,
        @Nullable CancellationSignal cancellationSignal) 

Source Link

Document

Query the given URI, returning a Cursor over the result set with optional support for cancellation.

Usage

From source file:com.whiuk.philip.opensmime.PathConverter.java

private static FileInformation handleContentScheme(Context context, Uri uri) {
    try {/*w  w  w  . j a va 2s. com*/
        ContentResolver contentResolver = context.getContentResolver();

        // all fields for one document
        Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {

            // Note it's called "Display Name".  This is
            // provider-specific, and might not necessarily be the file name.
            String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            String mimeType = cursor
                    .getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
            InputStream stream = contentResolver.openInputStream(uri);
            File tmpFile = copyToTempFile(context, stream);
            return new FileInformation(tmpFile, displayName, mimeType);
        }

    } catch (IOException e) {
        Log.e(OpenSMIME.LOG_TAG, "error in PathConverter.handleContentScheme", e);
    }

    return null;
}

From source file:de.fau.cs.mad.smile.android.encryption.PathConverter.java

private static FileInformation handleContentScheme(Context context, Uri uri) {
    try {//  w ww  .jav  a2  s.c o  m
        ContentResolver contentResolver = context.getContentResolver();

        // all fields for one document
        Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {

            // Note it's called "Display Name".  This is
            // provider-specific, and might not necessarily be the file name.
            String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            String mimeType = cursor
                    .getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
            InputStream stream = contentResolver.openInputStream(uri);
            File tmpFile = copyToTempFile(context, stream);
            return new FileInformation(tmpFile, displayName, mimeType);
        }

    } catch (IOException e) {
        Log.e(SMileCrypto.LOG_TAG, "error in PathConverter.handleContentScheme", e);
    }

    return null;
}

From source file:com.github.chenxiaolong.dualbootpatcher.FileUtils.java

public static UriMetadata[] queryUriMetadata(ContentResolver cr, Uri... uris) {
    ThreadUtils.enforceExecutionOnNonMainThread();

    UriMetadata[] metadatas = new UriMetadata[uris.length];
    for (int i = 0; i < metadatas.length; i++) {
        UriMetadata metadata = new UriMetadata();
        metadatas[i] = metadata;/*  ww  w  .  j a v a  2s  .co m*/
        metadata.uri = uris[i];
        metadata.mimeType = cr.getType(metadata.uri);

        if (SAF_SCHEME.equals(metadata.uri.getScheme())) {
            Cursor cursor = cr.query(metadata.uri, null, null, null, null, null);
            try {
                if (cursor != null && cursor.moveToFirst()) {
                    int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                    int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);

                    metadata.displayName = cursor.getString(nameIndex);
                    if (cursor.isNull(sizeIndex)) {
                        metadata.size = -1;
                    } else {
                        metadata.size = cursor.getLong(sizeIndex);
                    }
                }
            } finally {
                IOUtils.closeQuietly(cursor);
            }
        } else if (FILE_SCHEME.equals(metadata.uri.getScheme())) {
            metadata.displayName = metadata.uri.getLastPathSegment();
            metadata.size = new File(metadata.uri.getPath()).length();
        } else {
            throw new IllegalArgumentException("Cannot handle URI: " + metadata.uri);
        }
    }

    return metadatas;
}

From source file:com.google.android.apps.muzei.SourceSubscriberService.java

@Override
protected void onHandleIntent(Intent intent) {
    if (intent == null || intent.getAction() == null) {
        return;/*from  ww w.  j  a va  2s .c  o  m*/
    }

    String action = intent.getAction();
    if (!ACTION_PUBLISH_STATE.equals(action)) {
        return;
    }
    // Handle API call from source
    String token = intent.getStringExtra(EXTRA_TOKEN);
    ComponentName selectedSource = SourceManager.getSelectedSource(this);
    if (selectedSource == null || !TextUtils.equals(token, selectedSource.flattenToShortString())) {
        Log.w(TAG, "Dropping update from non-selected source, token=" + token + " does not match token for "
                + selectedSource);
        return;
    }

    SourceState state = null;
    if (intent.hasExtra(EXTRA_STATE)) {
        Bundle bundle = intent.getBundleExtra(EXTRA_STATE);
        if (bundle != null) {
            state = SourceState.fromBundle(bundle);
        }
    }

    if (state == null) {
        // If there is no state, there is nothing to change
        return;
    }

    ContentValues values = new ContentValues();
    values.put(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME, selectedSource.flattenToShortString());
    values.put(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED, true);
    values.put(MuzeiContract.Sources.COLUMN_NAME_DESCRIPTION, state.getDescription());
    values.put(MuzeiContract.Sources.COLUMN_NAME_WANTS_NETWORK_AVAILABLE, state.getWantsNetworkAvailable());
    JSONArray commandsSerialized = new JSONArray();
    int numSourceActions = state.getNumUserCommands();
    boolean supportsNextArtwork = false;
    for (int i = 0; i < numSourceActions; i++) {
        UserCommand command = state.getUserCommandAt(i);
        if (command.getId() == MuzeiArtSource.BUILTIN_COMMAND_ID_NEXT_ARTWORK) {
            supportsNextArtwork = true;
        } else {
            commandsSerialized.put(command.serialize());
        }
    }
    values.put(MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND, supportsNextArtwork);
    values.put(MuzeiContract.Sources.COLUMN_NAME_COMMANDS, commandsSerialized.toString());
    ContentResolver contentResolver = getContentResolver();
    Cursor existingSource = contentResolver.query(MuzeiContract.Sources.CONTENT_URI,
            new String[] { BaseColumns._ID }, MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME + "=?",
            new String[] { selectedSource.flattenToShortString() }, null, null);
    if (existingSource != null && existingSource.moveToFirst()) {
        Uri sourceUri = ContentUris.withAppendedId(MuzeiContract.Sources.CONTENT_URI,
                existingSource.getLong(0));
        contentResolver.update(sourceUri, values, null, null);
    } else {
        contentResolver.insert(MuzeiContract.Sources.CONTENT_URI, values);
    }
    if (existingSource != null) {
        existingSource.close();
    }

    Artwork artwork = state.getCurrentArtwork();
    if (artwork != null) {
        artwork.setComponentName(selectedSource);
        contentResolver.insert(MuzeiContract.Artwork.CONTENT_URI, artwork.toContentValues());

        // Download the artwork contained from the newly published SourceState
        startService(TaskQueueService.getDownloadCurrentArtworkIntent(this));
    }
}