Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import android.provider.OpenableColumns;

public class Main {
    /**
     * Get a uri's user-friendly display name
     * 
     * @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;
    }
}