Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

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

import android.provider.MediaStore;

import java.io.File;

public class Main {
    /**
     * Return file display name from Uri
     *
     * @param context Context
     * @param uri URI of the file
     * @return return file display name
     */
    public static String getFileDisplayNameFromUri(Context context, Uri uri) {
        String displayName = "";
        if (uri.getScheme().toString().compareTo("content") == 0) {
            Cursor cursor = null;
            try {
                cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                    displayName = cursor.getString(column_index);
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        } else if (uri.getScheme().toString().compareTo("file") == 0) {
            final File file = new File(uri.getPath());
            displayName = file.getName();
        }
        return displayName;
    }
}