List of usage examples for android.content.res AssetFileDescriptor getFileDescriptor
public FileDescriptor getFileDescriptor()
From source file:android.widget.TiVideoView4.java
private void openVideo() { if (mUri == null || mSurfaceHolder == null) { // not ready for playback just yet, will try again later return;// www . jav a 2 s . com } // Tell the music playback service to pause // TODO: these constants need to be published somewhere in the framework. Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra("command", "pause"); getContext().sendBroadcast(i); if (mMediaPlayer != null) { mMediaPlayer.reset(); mMediaPlayer.release(); mMediaPlayer = null; } try { mMediaPlayer = new MediaPlayer(); mMediaPlayer.setOnPreparedListener(mPreparedListener); mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); mIsPrepared = false; Log.v(TAG, "reset duration to -1 in openVideo"); mDuration = -1; mMediaPlayer.setOnCompletionListener(mCompletionListener); mMediaPlayer.setOnErrorListener(mErrorListener); mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); mCurrentBufferPercentage = 0; if (URLUtil.isAssetUrl(mUri.toString())) { // DST: 20090606 detect asset url AssetFileDescriptor afd = null; try { String path = mUri.toString().substring("file:///android_asset/".length()); afd = getContext().getAssets().openFd(path); mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); } finally { if (afd != null) { afd.close(); } } } else { setDataSource(); } mMediaPlayer.setDisplay(mSurfaceHolder); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setScreenOnWhilePlaying(true); mMediaPlayer.prepareAsync(); attachMediaController(); } catch (IOException ex) { Log.w(TAG, "Unable to open content: " + mUri, ex); return; } catch (IllegalArgumentException ex) { Log.w(TAG, "Unable to open content: " + mUri, ex); return; } }
From source file:com.commontime.cordova.audio.AudioPlayer.java
/** * load audio file//from w w w . j a va 2 s. com * @throws IOException * @throws IllegalStateException * @throws SecurityException * @throws IllegalArgumentException */ private void loadAudioFile(String file) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException { if (this.isStreaming(file)) { this.player.setDataSource(file); this.player.setAudioStreamType(AudioManager.STREAM_MUSIC); //if it's a streaming file, play mode is implied this.setMode(MODE.PLAY); this.setState(STATE.MEDIA_STARTING); this.player.setOnPreparedListener(this); this.player.prepareAsync(); } else { if (file.startsWith("/android_asset/")) { String f = file.substring(15); android.content.res.AssetFileDescriptor fd = this.handler.cordova.getActivity().getAssets() .openFd(f); this.player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength()); } else { File fp = new File(file); if (fp.exists()) { FileInputStream fileInputStream = new FileInputStream(file); this.player.setDataSource(fileInputStream.getFD()); fileInputStream.close(); } else { this.player.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/" + file); } } this.setState(STATE.MEDIA_STARTING); this.player.setOnPreparedListener(this); this.player.prepare(); // Get duration this.duration = getDurationInSeconds(); } }
From source file:inc.bait.jubilee.ui.fragments.ContactsListFragment.java
private Bitmap loadContactPhotoThumbnail(String photoData, int imageSize) { if (!isAdded() || getActivity() == null) { return null; }/* w w w . j ava2 s .c o m*/ AssetFileDescriptor afd = null; try { Uri thumbUri; if (ApiHelper.hasHoneycomb()) { thumbUri = Uri.parse(photoData); } else { final Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, photoData); thumbUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY); } afd = getActivity().getContentResolver().openAssetFileDescriptor(thumbUri, "r"); FileDescriptor fileDescriptor = afd.getFileDescriptor(); if (fileDescriptor != null) { return ImgLoader.decodeSampledBitmapFromDescriptor(fileDescriptor, imageSize, imageSize); } } catch (FileNotFoundException e) { if (BuildConfig.DEBUG) { Log.d(TAG, "Contact photo thumbnail not found for contact " + photoData + ": " + e.toString()); } } finally { if (afd != null) { try { afd.close(); } catch (IOException e) { } } } return null; }
From source file:fr.bde_eseo.eseomega.GantierActivity.java
@Override public void onResume() { super.onResume(); /*//from w w w . j a v a2 s. co m senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_GAME);*/ initListeners(); // wait for one second until gyroscope and magnetometer/accelerometer // data is initialised then scedule the complementary filter task fuseTimer.scheduleAtFixedRate(new calculateFusedOrientationTask(), 1000, TIME_CONSTANT); // if not playing if (!mediaPlayer.isPlaying()) { AssetFileDescriptor descriptor = null; try { descriptor = getAssets().openFd("wabe.mp3"); mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); descriptor.close(); mediaPlayer.prepare(); mediaPlayer.setLooping(true); } catch (IOException e) { e.printStackTrace(); } } // Start music ! mediaPlayer.start(); }
From source file:com.kjsaw.alcosys.ibacapp.IBAC.java
private void initBeepSound() { if (mediaPlayerBeep == null) { setVolumeControlStream(AudioManager.STREAM_MUSIC); mediaPlayerBeep = new MediaPlayer(); mediaPlayerBeep.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayerBeep.setOnCompletionListener(beepListener); AssetFileDescriptor fileBeep = getResources().openRawResourceFd(R.raw.beep); try {//w w w . ja v a 2s. c om mediaPlayerBeep.setDataSource(fileBeep.getFileDescriptor(), fileBeep.getStartOffset(), fileBeep.getLength()); fileBeep.close(); mediaPlayerBeep.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayerBeep.prepare(); } catch (IOException e) { mediaPlayerBeep = null; } } }
From source file:com.kjsaw.alcosys.ibacapp.IBAC.java
private void initCaptureSound() { if (mediaPlayerCapture == null) { setVolumeControlStream(AudioManager.STREAM_MUSIC); mediaPlayerCapture = new MediaPlayer(); mediaPlayerCapture.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayerCapture.setOnCompletionListener(captureListener); AssetFileDescriptor fileBeep = getResources().openRawResourceFd(R.raw.camera_flash); try {/*from www .ja v a 2 s . co m*/ mediaPlayerCapture.setDataSource(fileBeep.getFileDescriptor(), fileBeep.getStartOffset(), fileBeep.getLength()); fileBeep.close(); mediaPlayerCapture.setVolume(CAPTURE_VOLUME, CAPTURE_VOLUME); mediaPlayerCapture.prepare(); } catch (IOException e) { mediaPlayerCapture = null; } } }
From source file:fr.jbteam.jabboid.core.ContactDetailFragment.java
/** * Decodes and returns the contact's thumbnail image. * @param contactUri The Uri of the contact containing the image. * @param imageSize The desired target width and height of the output image in pixels. * @return If a thumbnail image exists for the contact, a Bitmap image, otherwise null. *///from w ww . j a v a 2s. c om @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private Bitmap loadContactPhoto(Uri contactUri, int imageSize) { // Ensures the Fragment is still added to an activity. As this method is called in a // background thread, there's the possibility the Fragment is no longer attached and // added to an activity. If so, no need to spend resources loading the contact photo. if (!isAdded() || getActivity() == null) { return null; } // Instantiates a ContentResolver for retrieving the Uri of the image final ContentResolver contentResolver = getActivity().getContentResolver(); // Instantiates an AssetFileDescriptor. Given a content Uri pointing to an image file, the // ContentResolver can return an AssetFileDescriptor for the file. AssetFileDescriptor afd = null; if (Utils.hasICS()) { // On platforms running Android 4.0 (API version 14) and later, a high resolution image // is available from Photo.DISPLAY_PHOTO. try { // Constructs the content Uri for the image Uri displayImageUri = Uri.withAppendedPath(contactUri, Photo.DISPLAY_PHOTO); // Retrieves an AssetFileDescriptor from the Contacts Provider, using the // constructed Uri afd = contentResolver.openAssetFileDescriptor(displayImageUri, "r"); // If the file exists if (afd != null) { // Reads and decodes the file to a Bitmap and scales it to the desired size return ImageLoader.decodeSampledBitmapFromDescriptor(afd.getFileDescriptor(), imageSize, imageSize); } } catch (FileNotFoundException e) { // Catches file not found exceptions /*if (BuildConfig.DEBUG) { // Log debug message, this is not an error message as this exception is thrown // when a contact is legitimately missing a contact photo (which will be quite // frequently in a long contacts list). Log.d(TAG, "Contact photo not found for contact " + contactUri.toString() + ": " + e.toString()); }*/ } finally { // Once the decode is complete, this closes the file. You must do this each time // you access an AssetFileDescriptor; otherwise, every image load you do will open // a new descriptor. if (afd != null) { try { afd.close(); } catch (IOException e) { // Closing a file descriptor might cause an IOException if the file is // already closed. Nothing extra is needed to handle this. } } } } // If the platform version is less than Android 4.0 (API Level 14), use the only available // image URI, which points to a normal-sized image. try { // Constructs the image Uri from the contact Uri and the directory twig from the // Contacts.Photo table Uri imageUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY); // Retrieves an AssetFileDescriptor from the Contacts Provider, using the constructed // Uri afd = getActivity().getContentResolver().openAssetFileDescriptor(imageUri, "r"); // If the file exists if (afd != null) { // Reads the image from the file, decodes it, and scales it to the available screen // area return ImageLoader.decodeSampledBitmapFromDescriptor(afd.getFileDescriptor(), imageSize, imageSize); } } catch (FileNotFoundException e) { // Catches file not found exceptions /*if (BuildConfig.DEBUG) { // Log debug message, this is not an error message as this exception is thrown // when a contact is legitimately missing a contact photo (which will be quite // frequently in a long contacts list). Log.d(TAG, "Contact photo not found for contact " + contactUri.toString() + ": " + e.toString()); }*/ } finally { // Once the decode is complete, this closes the file. You must do this each time you // access an AssetFileDescriptor; otherwise, every image load you do will open a new // descriptor. if (afd != null) { try { afd.close(); } catch (IOException e) { // Closing a file descriptor might cause an IOException if the file is // already closed. Ignore this. } } } // If none of the case selectors match, returns null. return null; }
From source file:android.com.example.contactslist.ui.ContactDetailFragment.java
/** * Decodes and returns the contact's thumbnail image. * @param contactUri The Uri of the contact containing the image. * @param imageSize The desired target width and height of the output image in pixels. * @return If a thumbnail image exists for the contact, a Bitmap image, otherwise null. *///www . j a va 2s . c o m @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private Bitmap loadContactPhoto(Uri contactUri, int imageSize) { // Ensures the Fragment is still added to an activity. As this method is called in a // background thread, there's the possibility the Fragment is no longer attached and // added to an activity. If so, no need to spend resources loading the contact photo. if (!isAdded() || getActivity() == null) { return null; } // Instantiates a ContentResolver for retrieving the Uri of the image final ContentResolver contentResolver = getActivity().getContentResolver(); // Instantiates an AssetFileDescriptor. Given a content Uri pointing to an image file, the // ContentResolver can return an AssetFileDescriptor for the file. AssetFileDescriptor afd = null; if (Utils.hasICS()) { // On platforms running Android 4.0 (API version 14) and later, a high resolution image // is available from Photo.DISPLAY_PHOTO. try { // Constructs the content Uri for the image Uri displayImageUri = Uri.withAppendedPath(contactUri, Photo.DISPLAY_PHOTO); // Retrieves an AssetFileDescriptor from the Contacts Provider, using the // constructed Uri afd = contentResolver.openAssetFileDescriptor(displayImageUri, "r"); // If the file exists if (afd != null) { // Reads and decodes the file to a Bitmap and scales it to the desired size return ImageLoader.decodeSampledBitmapFromDescriptor(afd.getFileDescriptor(), imageSize, imageSize); } } catch (FileNotFoundException e) { // Catches file not found exceptions if (BuildConfig.DEBUG) { // Log debug message, this is not an error message as this exception is thrown // when a contact is legitimately missing a contact photo (which will be quite // frequently in a long contacts list). Log.d(TAG, "Contact photo not found for contact " + contactUri.toString() + ": " + e.toString()); } } finally { // Once the decode is complete, this closes the file. You must do this each time // you access an AssetFileDescriptor; otherwise, every image load you do will open // a new descriptor. if (afd != null) { try { afd.close(); } catch (IOException e) { // Closing a file descriptor might cause an IOException if the file is // already closed. Nothing extra is needed to handle this. } } } } // If the platform version is less than Android 4.0 (API Level 14), use the only available // image URI, which points to a normal-sized image. try { // Constructs the image Uri from the contact Uri and the directory twig from the // Contacts.Photo table Uri imageUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY); // Retrieves an AssetFileDescriptor from the Contacts Provider, using the constructed // Uri afd = getActivity().getContentResolver().openAssetFileDescriptor(imageUri, "r"); // If the file exists if (afd != null) { // Reads the image from the file, decodes it, and scales it to the available screen // area return ImageLoader.decodeSampledBitmapFromDescriptor(afd.getFileDescriptor(), imageSize, imageSize); } } catch (FileNotFoundException e) { // Catches file not found exceptions if (BuildConfig.DEBUG) { // Log debug message, this is not an error message as this exception is thrown // when a contact is legitimately missing a contact photo (which will be quite // frequently in a long contacts list). Log.d(TAG, "Contact photo not found for contact " + contactUri.toString() + ": " + e.toString()); } } finally { // Once the decode is complete, this closes the file. You must do this each time you // access an AssetFileDescriptor; otherwise, every image load you do will open a new // descriptor. if (afd != null) { try { afd.close(); } catch (IOException e) { // Closing a file descriptor might cause an IOException if the file is // already closed. Ignore this. } } } // If none of the case selectors match, returns null. return null; }
From source file:com.example.fragmentexercise.ContactsAdapter.java
/** * Load a contact photo thumbnail and return it as a Bitmap, resizing the * image to the provided image dimensions as needed. * //from www.ja va 2s . c o m * @param photoData * photo ID Prior to Honeycomb, the contact's _ID value. For * Honeycomb and later, the value of PHOTO_THUMBNAIL_URI. * @return A thumbnail Bitmap, sized to the provided width and height. * Returns null if the thumbnail is not found. */ private Bitmap loadContactPhotoThumbnail(String photoData) { // Creates an asset file descriptor for the thumbnail file. AssetFileDescriptor afd = null; // try-catch block for file not found try { // Creates a holder for the URI. Uri thumbUri; // If Android 3.0 or later if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Sets the URI from the incoming PHOTO_THUMBNAIL_URI thumbUri = Uri.parse(photoData); } else { // Prior to Android 3.0, constructs a photo Uri using _ID /* * Creates a contact URI from the Contacts content URI incoming * photoData (_ID) */ final Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, photoData); /* * Creates a photo URI by appending the content URI of * Contacts.Photo. */ thumbUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY); } /* * Retrieves an AssetFileDescriptor object for the thumbnail URI * using ContentResolver.openAssetFileDescriptor */ afd = mContext.getContentResolver().openAssetFileDescriptor(thumbUri, "r"); /* * Gets a file descriptor from the asset file descriptor. This * object can be used across processes. */ FileDescriptor fileDescriptor = afd.getFileDescriptor(); // Decode the photo file and return the result as a Bitmap // If the file descriptor is valid if (fileDescriptor != null) { // Decodes the bitmap return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, null); } // If the file isn't found } catch (FileNotFoundException e) { /* * Handle file not found errors */ } // In all cases, close the asset file descriptor finally { if (afd != null) { try { afd.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:br.com.mybaby.contatos.ContactDetailFragment.java
/** * Decodes and returns the contact's thumbnail image. * @param contactUri The Uri of the contact containing the image. * @param imageSize The desired target width and height of the output image in pixels. * @return If a thumbnail image exists for the contact, a Bitmap image, otherwise null. *//* www . j a v a2 s . com*/ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private Bitmap loadContactPhoto(Uri contactUri, int imageSize) { // Ensures the Fragment is still added to an activity. As this method is called in a // background thread, there's the possibility the Fragment is no longer attached and // added to an activity. If so, no need to spend resources loading the contact photo. if (!isAdded() || getActivity() == null) { return null; } // Instantiates a ContentResolver for retrieving the Uri of the image final ContentResolver contentResolver = getActivity().getContentResolver(); // Instantiates an AssetFileDescriptor. Given a content Uri pointing to an image file, the // ContentResolver can return an AssetFileDescriptor for the file. AssetFileDescriptor afd = null; if (Util.hasICS()) { // On platforms running Android 4.0 (API version 14) and later, a high resolution image // is available from Photo.DISPLAY_PHOTO. try { // Constructs the content Uri for the image Uri displayImageUri = Uri.withAppendedPath(contactUri, Photo.DISPLAY_PHOTO); // Retrieves an AssetFileDescriptor from the Contacts Provider, using the // constructed Uri afd = contentResolver.openAssetFileDescriptor(displayImageUri, "r"); // If the file exists if (afd != null) { // Reads and decodes the file to a Bitmap and scales it to the desired size return ImageLoader.decodeSampledBitmapFromDescriptor(afd.getFileDescriptor(), imageSize, imageSize); } } catch (FileNotFoundException e) { // Catches file not found exceptions if (BuildConfig.DEBUG) { // Log debug message, this is not an error message as this exception is thrown // when a contact is legitimately missing a contact photo (which will be quite // frequently in a long contacts list). Log.d(TAG, "Contact photo not found for contact " + contactUri.toString() + ": " + e.toString()); } } finally { // Once the decode is complete, this closes the file. You must do this each time // you access an AssetFileDescriptor; otherwise, every image load you do will open // a new descriptor. if (afd != null) { try { afd.close(); } catch (IOException e) { // Closing a file descriptor might cause an IOException if the file is // already closed. Nothing extra is needed to handle this. } } } } // If the platform version is less than Android 4.0 (API Level 14), use the only available // image URI, which points to a normal-sized image. try { // Constructs the image Uri from the contact Uri and the directory twig from the // Contacts.Photo table Uri imageUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY); // Retrieves an AssetFileDescriptor from the Contacts Provider, using the constructed // Uri afd = getActivity().getContentResolver().openAssetFileDescriptor(imageUri, "r"); // If the file exists if (afd != null) { // Reads the image from the file, decodes it, and scales it to the available screen // area return ImageLoader.decodeSampledBitmapFromDescriptor(afd.getFileDescriptor(), imageSize, imageSize); } } catch (FileNotFoundException e) { // Catches file not found exceptions if (BuildConfig.DEBUG) { // Log debug message, this is not an error message as this exception is thrown // when a contact is legitimately missing a contact photo (which will be quite // frequently in a long contacts list). Log.d(TAG, "Contact photo not found for contact " + contactUri.toString() + ": " + e.toString()); } } finally { // Once the decode is complete, this closes the file. You must do this each time you // access an AssetFileDescriptor; otherwise, every image load you do will open a new // descriptor. if (afd != null) { try { afd.close(); } catch (IOException e) { // Closing a file descriptor might cause an IOException if the file is // already closed. Ignore this. } } } // If none of the case selectors match, returns null. return null; }