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;

public class Main {
    public static String uriTitle(Context context, String contentUri) {
        String result = null;
        String[] p = { MediaStore.MediaColumns.TITLE };
        Uri uri = Uri.parse(contentUri);
        if (!uri.getScheme().equals("content")) {
            return (fileUriTitle(context, contentUri));
        }
        Cursor cursor = context.getContentResolver().query(uri, p, // which columns
                null, // which rows (all rows)
                null, // selection args (none)
                null); // order-by clause (ascending by name)
        if (cursor != null) {
            int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
            if (cursor.moveToFirst()) {
                result = cursor.getString(iColumn);
            }
        }
        return (result);
    }

    private static String fileUriTitle(Context context, String contentUri) {
        String result = null;
        String[] p = { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE };
        Uri uri = Uri.parse(contentUri);
        String path = uri.getPath();
        String last = Uri.parse(path).getLastPathSegment();
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, p, // which columns
                MediaStore.MediaColumns.DISPLAY_NAME + "='" + last + "'", // which rows
                null, // selection args (none)
                null); // order-by clause (ascending by name)
        if (cursor != null) {
            int tcol = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
            if (cursor.moveToFirst()) {
                result = cursor.getString(tcol);
            }
        }
        return (result);
    }
}