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:Main.java

static String getFileName(Context context, Uri uri) {
    String result = null;/*from w  ww. j  av  a  2s. c o  m*/
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:Main.java

public static String uriToString(Context context, Uri uri) {
    String scheme = uri.getScheme();
    if (scheme != null) {
        if (scheme.equals("http") || scheme.equals("https")) {
            return uri.toString();
        } else if (scheme.equals("content") || scheme.equals("file")) {
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[] { OpenableColumns.DISPLAY_NAME }, null, null, null);
            if (cursor.moveToNext()) {
                String name = cursor.getString(0);
                cursor.close();/*from ww w.j  ava  2  s.c o  m*/
                return name;
            }
            cursor.close();
            return uri.getPath();
        }
    }
    return uri.toString();
}

From source file:Main.java

public static String getFileName(Uri uri, Context context) {
    String result = null;/*from  w  w  w  .  ja  v a  2 s . c o m*/
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                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:Main.java

/**
 * Get a uri's user-friendly display name
 * /*from ww w. j  ava2  s  .c o m*/
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return a user-friendly display name
 */
public static String getUriDisplayName(Context context, Uri uri) {
    String displayName = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] proj = { OpenableColumns.DISPLAY_NAME };
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);

        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            displayName = cursor.getString(columnIndex);

            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        displayName = uri.getLastPathSegment();
    }

    return displayName;
}

From source file:Main.java

private static String getFileName(Context context, Uri uri) {
    Log.d("suka", uri.getScheme() + " : " + context.getContentResolver().getType(uri));
    String result = null;//from   ww  w .ja  v  a 2s. c  o  m
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.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) {
        Log.d("suka", "res " + uri.getPath());
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Query the server app to get the file's display name.
 *//* w  ww . j a  v  a  2  s.co m*/
public static String getUriFileName(Context context, Uri uri) {
    if (uri == null) {
        return null;
    }

    if (uri.getScheme().equals("file")) {
        return uri.getLastPathSegment();
    }

    if (uri.getScheme().equals("content")) {
        Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);

        //Get the column index of the data in the Cursor,
        //move to the first row in the Cursor, get the data, and display it.
        int name_index = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        //int size_index = returnCursor.getColumnIndex(OpenableColumns.SIZE);

        if (name_index < 0) {
            return null;
        }

        returnCursor.moveToFirst();

        //return returnCursor.getLong(size_index)
        return returnCursor.getString(name_index);
    }
    return null;
}

From source file:Main.java

public static String resolveFileName(Uri uri, Activity activity) {
    String filename = null;//from w  w  w  .  j  a  v  a 2  s  .  c om
    String uriString = uri.toString();

    if (uriString.startsWith("content://")) {
        Cursor cursor = null;
        try {
            cursor = activity.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                String mimeType = MimeTypeMap.getSingleton()
                        .getExtensionFromMimeType(activity.getContentResolver().getType(uri));
                filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

                if (mimeType != null && !filename.endsWith("." + mimeType)) {
                    filename += "." + mimeType;
                }
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uriString.startsWith("file://")) {
        filename = (new File(uriString)).getName();
    }
    return filename;
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

public static String resolveFileName(Uri uri, ContentResolver contentResolver) {
    String uriString = uri.toString();
    if (isContentUri(uriString)) {
        try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }/*  w ww.ja  v a  2 s .c  om*/
        }
    } else if (isFileUri(uriString)) {
        return new File(uriString).getName();
    }
    return null;
}

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

private static FileInformation handleContentScheme(Context context, Uri uri) {
    try {//from   w w  w . j  a va  2 s  .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 {/*from   w  w w  . ja v a 2 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;
}