Example usage for android.provider OpenableColumns DISPLAY_NAME

List of usage examples for android.provider OpenableColumns DISPLAY_NAME

Introduction

In this page you can find the example usage for android.provider OpenableColumns DISPLAY_NAME.

Prototype

String DISPLAY_NAME

To view the source code for android.provider OpenableColumns DISPLAY_NAME.

Click Source Link

Document

The human-friendly name of file.

Usage

From source file:com.github.guwenk.smuradio.SignInDialog.java

public String getFileName(Uri uri) {
    String result = null;//from   w  w w  .  j av a 2 s.c o m
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:org.mklab.mikity.android.FileSelectionFragment.java

/**
 * URI?????//  ww  w  .ja  v a2 s  . com
 * 
 * @param sourceFileUri URI
 * @param sourceId ID
 */
public void loadSourceData(Uri sourceFileUri, String sourceId) {
    if (sourceFileUri == null) {
        return;
    }

    final String sourceFileName;
    final InputStream sourceStream;

    if ("content".equals(sourceFileUri.getScheme())) { //$NON-NLS-1$
        // URI?????
        try {
            sourceStream = this.mainActivity.getContentResolver().openInputStream(sourceFileUri);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        final Cursor cursor = this.mainActivity.getContentResolver().query(sourceFileUri, null, null, null,
                null);
        cursor.moveToFirst();
        sourceFileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
        cursor.close();
        // URI???????????
    } else {
        final String sourceDataFilePath = sourceFileUri.getPath();
        try {
            sourceStream = new FileInputStream(sourceDataFilePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        final String[] parts = sourceDataFilePath.split("/"); //$NON-NLS-1$
        sourceFileName = parts[parts.length - 1];
    }

    this.canvasFragment.loadSourceDataInBackground(sourceStream, sourceFileUri.getPath(), sourceFileName,
            sourceId, this);
    // sourceStream has been already closed in the loadSourceData method. 
}

From source file:org.totschnig.myexpenses.dialog.DialogUtils.java

/**
 * @return display name for document stored at mUri.
 * Returns null if accessing mUri raises {@link SecurityException}
 *//* w w  w.java 2s . c  o m*/
@SuppressLint("NewApi")
public static String getDisplayName(Uri uri) {

    if (!"file".equalsIgnoreCase(uri.getScheme()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // The query, since it only applies to a single document, will only return
        // one row. There's no need to filter, sort, or select fields, since we want
        // all fields for one document.
        try {
            Cursor cursor = MyApplication.getInstance().getContentResolver().query(uri, null, null, null, null,
                    null);

            if (cursor != null) {
                try {
                    if (cursor.moveToFirst()) {
                        // Note it's called "Display Name".  This is
                        // provider-specific, and might not necessarily be the file name.
                        int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                        if (columnIndex != -1) {
                            String displayName = cursor.getString(columnIndex);
                            if (displayName != null) {
                                return displayName;
                            }
                        }
                    }
                } catch (Exception e) {
                } finally {
                    cursor.close();
                }
            }
        } catch (SecurityException e) {
            //this can happen if the user has restored a backup and
            //we do not have a persistable permision
            //return null;
        }
    }
    List<String> filePathSegments = uri.getPathSegments();
    if (!filePathSegments.isEmpty()) {
        return filePathSegments.get(filePathSegments.size() - 1);
    } else {
        return "UNKNOWN";
    }
}

From source file:de.j4velin.encrypter.MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (requestCode == REQUEST_INPUT && resultCode == RESULT_OK && data != null) {
        Uri uri = data.getData();/*ww w.  j a  va  2  s  . co m*/
        String inputName = null;
        int inputSize = -1;
        String inputType = getContentResolver().getType(uri);
        try (Cursor cursor = getContentResolver().query(uri, null, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                inputName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
                if (!cursor.isNull(sizeIndex)) {
                    inputSize = cursor.getInt(sizeIndex);
                }
            }
        }
        File input = new File(-1, inputName, inputType, uri, inputSize, false);
        try {
            CryptoUtil.encrypt(MainActivity.this, input);
        } catch (GeneralSecurityException e) {
            Snackbar.make(coordinatorLayout, getString(R.string.error_security, e.getMessage()),
                    Snackbar.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            Snackbar.make(coordinatorLayout, R.string.error_file_not_found, Snackbar.LENGTH_LONG).show();
        } catch (IOException e) {
            Snackbar.make(coordinatorLayout, getString(R.string.error_io, e.getMessage()), Snackbar.LENGTH_LONG)
                    .show();
        }
    } else

    {
        super.onActivityResult(requestCode, resultCode, data);
    }

}

From source file:org.rm3l.ddwrt.mgmt.AbstractRouterMgmtDialogFragment.java

/**
 * Receive the result from a previous call to
 * {@link #startActivityForResult(android.content.Intent, int)}.  This follows the
 * related Activity API as described there in
 * {@link android.app.Activity#onActivityResult(int, int, android.content.Intent)}.
 *
 * @param requestCode The integer request code originally supplied to
 *                    startActivityForResult(), allowing you to identify who this
 *                    result came from.//  ww w  .  ja  v  a 2  s. c  o  m
 * @param resultCode  The integer result code returned by the child activity
 *                    through its setResult().
 * @param resultData  An Intent, which can return result data to the caller
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    // The ACTION_OPEN_DOCUMENT intent was sent with the request code
    // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
    // response to some other intent, and the code below shouldn't run at all.

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri;
        if (resultData != null) {
            uri = resultData.getData();
            Log.i(LOG_TAG, "Uri: " + uri.toString());
            final AlertDialog d = (AlertDialog) getDialog();
            if (d != null) {

                final ContentResolver contentResolver = this.getSherlockActivity().getContentResolver();

                final Cursor uriCursor = contentResolver.query(uri, null, null, null, null);

                /*
                 * Get the column indexes of the data in the Cursor,
                 * move to the first row in the Cursor, get the data,
                 * and display it.
                 */
                final int nameIndex = uriCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                final int sizeIndex = uriCursor.getColumnIndex(OpenableColumns.SIZE);

                uriCursor.moveToFirst();

                //File size in bytes
                final long fileSize = uriCursor.getLong(sizeIndex);
                final String filename = uriCursor.getString(nameIndex);

                //Check file size
                if (fileSize > MAX_PRIVKEY_SIZE_BYTES) {
                    displayMessage(String.format("File '%s' too big (%s). Limit is %s", filename,
                            toHumanReadableByteCount(fileSize),
                            toHumanReadableByteCount(MAX_PRIVKEY_SIZE_BYTES)), ALERT);
                    return;
                }

                //Replace button hint message with file name
                final Button fileSelectorButton = (Button) d.findViewById(R.id.router_add_privkey);
                final CharSequence fileSelectorOriginalHint = fileSelectorButton.getHint();
                if (!Strings.isNullOrEmpty(filename)) {
                    fileSelectorButton.setHint(filename);
                }

                //Set file actual content in hidden field
                final TextView privKeyPath = (TextView) d.findViewById(R.id.router_add_privkey_path);
                try {
                    privKeyPath.setText(IOUtils.toString(contentResolver.openInputStream(uri)));
                } catch (IOException e) {
                    displayMessage("Error: " + e.getMessage(), ALERT);
                    e.printStackTrace();
                    fileSelectorButton.setHint(fileSelectorOriginalHint);
                }
            }
        }
    }
}

From source file:org.mklab.mikity.android.FileSelectionFragment.java

/**
 * URI?????//ww w .  j  a va2s .c  om
 * 
 * @param modelFileUri URI
 */
public void loadModelData(Uri modelFileUri) {
    if (modelFileUri == null) {
        return;
    }

    final InputStream modelInputStream;
    String fileName;

    if ("content".equals(modelFileUri.getScheme())) { //$NON-NLS-1$
        try {
            modelInputStream = this.mainActivity.getContentResolver().openInputStream(modelFileUri);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        final Cursor cursor = this.mainActivity.getContentResolver().query(modelFileUri, null, null, null,
                null);
        cursor.moveToFirst();
        fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
        cursor.close();
    } else {
        final String modelFilePath = modelFileUri.getPath();
        try {
            modelInputStream = new FileInputStream(modelFilePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        final String[] parts = modelFilePath.split("/"); //$NON-NLS-1$
        try {
            fileName = URLDecoder.decode(parts[parts.length - 1], "utf-8"); //$NON-NLS-1$
        } catch (UnsupportedEncodingException e) {
            fileName = parts[parts.length - 1];
        }
    }

    this.canvasFragment.loadModelDataInBackground(modelInputStream, fileName, this);
}

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;/*from  www.ja  v  a  2s.  c  om*/
        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.pdftron.pdf.utils.Utils.java

public static String getUriDisplayName(Context context, Uri contentUri) {
    String displayName = null;/* w w  w .jav  a2 s . c o m*/
    String[] projection = { OpenableColumns.DISPLAY_NAME };
    Cursor cursor = null;

    if (contentUri.getScheme().equalsIgnoreCase("file")) {
        return contentUri.getLastPathSegment();
    }
    try {
        cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int nameIndex = cursor.getColumnIndexOrThrow(projection[0]);
            if (nameIndex >= 0) {
                displayName = cursor.getString(nameIndex);
            }
        }
    } catch (Exception e) {
        displayName = null;
    }
    if (cursor != null) {
        cursor.close();
    }
    return displayName;
}

From source file:org.mklab.mikity.android.FileSelectionFragment.java

/**
 * URI?????//from   w w  w .  j  av a2  s .c om
 * 
 * @param modelFileUri URI
 */
public void saveModelData(Uri modelFileUri) {
    final OutputStream modelOutputStream;

    String fileName;
    if ("content".equals(modelFileUri.getScheme())) { //$NON-NLS-1$
        try {
            modelOutputStream = this.mainActivity.getContentResolver().openOutputStream(modelFileUri);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        final Cursor cursor = this.mainActivity.getContentResolver().query(modelFileUri, null, null, null,
                null);
        cursor.moveToFirst();
        fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
        cursor.close();
    } else {
        final String modelFilePath = modelFileUri.getPath();
        try {
            modelOutputStream = new FileOutputStream(modelFilePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        final String[] parts = modelFilePath.split("/"); //$NON-NLS-1$
        try {
            fileName = URLDecoder.decode(parts[parts.length - 1], "utf-8"); //$NON-NLS-1$
        } catch (UnsupportedEncodingException e) {
            fileName = parts[parts.length - 1];
        }
    }

    this.canvasFragment.saveModelDataInBackground(modelOutputStream, fileName, this);
}

From source file:de.spiritcroc.ownlog.ui.fragment.LogAttachmentsEditFragment.java

private String getAttachmentNameProposal(Intent intent) {
    Uri uri = intent.getData();//from w ww.  j  a  v  a  2  s. c o  m
    Cursor infoCursor = getActivity().getContentResolver().query(uri, null, null, null, null);
    String name = null;
    if (infoCursor != null) {
        infoCursor.moveToFirst();
        try {
            name = infoCursor.getString(infoCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
        } catch (Exception e) {
            e.printStackTrace();
        }
        infoCursor.close();
    }
    if (name == null) {
        Log.w(TAG, "addAttachment: query filename unsuccessful");
        return getString(R.string.edit_log_attachment_name_default);
    }
    return name;
}