...
public String handleFileUri(Uri beamUri) { // Get the path part of the URI String fileName = beamUri.getPath();
// Create a File object for this filename File copiedFile = new File(fileName);
// Get a string containing the file's parent directory return copiedFile.getParent();
} ...
Get the directory from a content URI
If the incoming Intent contains a content URI, the URI may point to a directory and file name stored in the MediaStore content provider. You can detect a content URI for MediaStore by testing the URI's authority value. A content URI for MediaStore may come from Android Beam file transfer or from another app, but in both cases you can retrieve a directory and file name for the content URI.
You can also receive an incoming ACTION_VIEW intent containing a content URI for a content provider other than MediaStore. In this case, the content URI doesn't contain the MediaStore authority value, and the content URI usually doesn't point to a directory.
Note: For Android Beam file transfer, you receive a content URI in the ACTION_VIEW intent if the first incoming file has a MIME type of "audio/*", "image/*", or "video/*", indicating that the file is media- related.
Android Beam file transfer indexes the media files it transfers by running Media Scanner on the directory where it stores transferred files. Media Scanner writes its results to the MediaStore content provider, then it passes a content URI for the first file back to Android Beam file transfer. This content URI is the one you receive in the notification Intent. To get the directory of the first file, you retrieve it from
MediaStore using the content URI.
Determine the content provider
To determine if you can retrieve a file directory from the content URI, determine the the content provider associated with the URI by calling Uri.getAuthority() to get the URI's authority. The result has two possible values:
MediaStore.AUTHORITY
The URI is for a file or files tracked by MediaStore. Retrieve the full file name from MediaStore, and get directory from the file name.
Any other authority value
A content URI from another content provider. Display the data associated with the content URI, but don't get the file directory.
To get the directory for a MediaStore content URI, run a query that specifies the incoming content URI for the Uri argument and the column MediaColumns.DATA for the projection. The returned Cursor contains the full path and name for the file represented by the URI. This path also contains all the other files that Android Beam file transfer just copied to the device.
The following snippet shows you how to test the authority of the content URI and retrieve the the path and file name for the transferred file:
Receiving Files from Another Device
138
Content from developer.android.com/training/beam-files/receive-files.html through their Creative Commons Attribution 2.5 license
...
public String handleContentUri(Uri beamUri) { // Position of the filename in the query Cursor int filenameIndex;
// File object for the filename File copiedFile;
// The filename stored in MediaStore String fileName;
// Test the authority of the URI
if (!TextUtils.equals(beamUri.getAuthority(), MediaStore.AUTHORITY)) { /*
* Handle content URIs for other content providers */
// For a MediaStore content URI } else {
// Get the column that contains the file name
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor pathCursor =
getContentResolver().query(beamUri, projection, null, null, null);
// Check for a valid cursor if (pathCursor != null &&
pathCursor.moveToFirst()) { // Get the column index in the Cursor filenameIndex = pathCursor.getColumnIndex(
MediaStore.MediaColumns.DATA);
// Get the full file name including path fileName = pathCursor.getString(filenameIndex);
// Create a File object for the filename copiedFile = new File(fileName);
// Return the parent directory of the file return new File(copiedFile.getParent());
} else {
// The query didn't work; return null return null;
} } } ...
To learn more about retrieving data from a content provider, see the section Retrieving Data from the Provider.
Building Apps with Multimedia
139
Content from developer.android.com/training/building-multimedia.html through their Creative Commons Attribution 2.5 license
46. Building Apps with Multimedia
Content from developer.android.com/training/building-multimedia.html through their Creative Commons Attribution 2.5 license
These classes teach you how to create rich multimedia apps that behave the way users expect.
Managing Audio Playback
140
Content from developer.android.com/training/managing-audio/index.html through their Creative Commons Attribution 2.5 license
47. Managing Audio Playback
Content from developer.android.com/training/managing-audio/index.html through their Creative Commons Attribution 2.5 license
If your app plays audio, it’s important that your users can control the audio in a predictable manner. To ensure a great user experience, it’s also important that your app manages the audio focus to ensure multiple apps aren’t playing audio at the same time.
After this class, you will be able to build apps that respond to hardware audio key presses, which request audio focus when playing audio, and which respond appropriately to changes in audio focus caused by the system or other applications.
Lessons
Controlling Your App’s Volume and Playback
Learn how to ensure your users can control the volume of your app using the hardware or software volume controls and where available the play, stop, pause, skip, and previous media playback keys.
Managing Audio Focus
With multiple apps potentially playing audio it's important to think about how they should interact.
To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback. Learn how to request the audio focus, listen for a loss of audio focus, and how to respond when that happens.
Dealing with Audio Output Hardware
Audio can be played from a number of sources. Learn how to find out where the audio is being played and how to handle a headset being disconnected during playback.
Dependencies and prerequisites
• Android 2.0 (API level 5) or higher
• Experience with Media Playback You should also read
• Services
Controlling Your App’s Volume and Playback
141
Content from developer.android.com/training/managing-audio/volume-playback.html through their Creative Commons Attribution 2.5 license
48. Controlling Your App’s Volume and Playback
Content from developer.android.com/training/managing-audio/volume-playback.html through their Creative Commons Attribution 2.5 license
A good user experience is a predictable one. If your app plays media it’s important that your users can control the volume of your app using the hardware or software volume controls of their device, bluetooth headset, or headphones.
Similarly, where appropriate and available, the play, stop, pause, skip, and previous media playback keys should perform their respective actions on the audio stream used by your app.