Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.ContentResolver;

import android.database.Cursor;

import android.net.Uri;

import android.provider.BaseColumns;
import android.provider.MediaStore;

public class Main {
    /**
     * Gets the Uri to a specific audio file
     * @param filePath The path of the file that we are looking up
     * @param contentResolver The content resolver that is used to perform the query
     * @return The Uri of the sound file
     */
    private static Uri getAudioUriFromFilePath(String filePath, ContentResolver contentResolver) {
        long audioId;
        Uri uri = MediaStore.Audio.Media.getContentUri("external");
        String[] projection = { BaseColumns._ID };
        Cursor cursor = contentResolver.query(uri, projection, MediaStore.MediaColumns.DATA + " LIKE ?",
                new String[] { filePath }, null);

        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(projection[0]);
            audioId = cursor.getLong(columnIndex);
            cursor.close();
            return Uri.parse(uri.toString() + "/" + audioId);
        }
        return null;
    }
}