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 {
    /**
     * Query the server app to get the file's display name.
     */
    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;
    }
}