Search in sources :

Example 61 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project android_frameworks_base by AOSPA.

the class DocumentsContract method openImageThumbnail.

/**
     * Open the given image for thumbnail purposes, using any embedded EXIF
     * thumbnail if available, and providing orientation hints from the parent
     * image.
     *
     * @hide
     */
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
    final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle extras = null;
    try {
        final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 270);
                break;
        }
        final long[] thumb = exif.getThumbnailRange();
        if (thumb != null) {
            return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
        }
    } catch (IOException e) {
    }
    return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) ExifInterface(android.media.ExifInterface) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException)

Example 62 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project Applozic-Android-SDK by AppLozic.

the class ContactsListFragment method loadContactPhotoThumbnail.

/**
 * Decodes and scales a contact's image from a file pointed to by a Uri in the contact's data,
 * and returns the result as a Bitmap. The column that contains the Uri varies according to the
 * platform version.
 *
 * @param photoData For platforms prior to Android 3.0, provide the Contact._ID column value.
 *                  For Android 3.0 and later, provide the Contact.PHOTO_THUMBNAIL_URI value.
 * @param imageSize The desired target width and height of the output image in pixels.
 * @return A Bitmap containing the contact's image, resized to fit the provided image size. If
 * no thumbnail exists, returns null.
 */
private Bitmap loadContactPhotoThumbnail(String photoData, int imageSize) {
    // added to an activity. If so, no need to spend resources loading the contact photo.
    if (!isAdded() || getActivity() == null) {
        return null;
    }
    // Instantiates an AssetFileDescriptor. Given a content Uri pointing to an image file, the
    // ContentResolver can return an AssetFileDescriptor for the file.
    AssetFileDescriptor afd = null;
    // Provider doesn't point to an existing file.
    try {
        Uri thumbUri;
        // If Android 3.0 or later, converts the Uri passed as a string to a Uri object.
        if (Utils.hasHoneycomb()) {
            thumbUri = Uri.parse(photoData);
        } else {
            // For versions prior to Android 3.0, appends the string argument to the content
            // Uri for the Contacts table.
            final Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, photoData);
            // Appends the content Uri for the Contacts.Photo table to the previously
            // constructed contact Uri to yield a content URI for the thumbnail image
            thumbUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY);
        }
        // Retrieves a file descriptor from the Contacts Provider. To learn more about this
        // feature, read the reference documentation for
        // ContentResolver#openAssetFileDescriptor.
        afd = getActivity().getContentResolver().openAssetFileDescriptor(thumbUri, "r");
        // Gets a FileDescriptor from the AssetFileDescriptor. A BitmapFactory object can
        // decode the contents of a file pointed to by a FileDescriptor into a Bitmap.
        FileDescriptor fileDescriptor = afd.getFileDescriptor();
        if (fileDescriptor != null) {
            // to the specified width and height
            return ImageLoader.decodeSampledBitmapFromDescriptor(fileDescriptor, imageSize, imageSize);
        }
    } catch (FileNotFoundException e) {
    // If the file pointed to by the thumbnail URI doesn't exist, or the file can't be
    // opened in "read" mode, ContentResolver.openAssetFileDescriptor throws a
    // FileNotFoundException.
    // if (BuildConfig.DEBUG) {
    // Log.d(TAG, "Contact photo thumbnail not found for contact " + photoData
    // + ": " + e.toString());
    // }
    } finally {
        // If an AssetFileDescriptor was returned, try to close it
        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 decoding failed, returns null
    return null;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Uri(android.net.Uri) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 63 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project Applozic-Android-SDK by AppLozic.

the class ContactUtils method loadContactPhoto.

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static Bitmap loadContactPhoto(Uri contactUri, int imageSize, Activity activity) {
    if (activity == null) {
        return null;
    }
    final ContentResolver contentResolver = activity.getContentResolver();
    try {
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, contactUri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
    } catch (Exception ex) {
    // Note:Catching exception, in some cases the code is throwing error.
    }
    AssetFileDescriptor afd = null;
    String displayPhoto = ContactsContract.Contacts.Photo.CONTENT_DIRECTORY;
    if (Utils.hasICS()) {
        displayPhoto = ContactsContract.Contacts.Photo.DISPLAY_PHOTO;
    }
    try {
        Uri imageUri = Uri.withAppendedPath(contactUri, displayPhoto);
        afd = activity.getContentResolver().openAssetFileDescriptor(imageUri, "r");
        if (afd != null) {
            return ImageLoader.decodeSampledBitmapFromDescriptor(afd.getFileDescriptor(), imageSize, imageSize);
        }
    } catch (FileNotFoundException ex) {
        Log.e(TAG, "Image not found error " + ex.getMessage());
    } finally {
        if (afd != null) {
            try {
                afd.close();
            } catch (IOException ex) {
                Log.e(TAG, "Image not found error " + ex.getMessage());
            }
        }
    }
    return null;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) TargetApi(android.annotation.TargetApi)

Example 64 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project HAR-Android by linw7.

the class BeeAndVibrateManager method playBee.

public static void playBee(final Context context) {
    AudioManager audioService = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
        // 检查当前是否是静音模式
        shouldPlayBeep = false;
    }
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer player) {
            player.seekTo(0);
        }
    });
    AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.dingding);
    try {
        mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        file.close();
        mediaPlayer.setVolume(0, 1);
        mediaPlayer.prepare();
    } catch (IOException ioe) {
        mediaPlayer = null;
    }
    if (shouldPlayBeep && mediaPlayer != null) {
        mediaPlayer.start();
    }
/*
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.stop();
                listener.onCompletion(mp);
            }
        });
        */
}
Also used : AudioManager(android.media.AudioManager) AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 65 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project xDrip by NightscoutFoundation.

the class AlertPlayer method setDataSource.

private boolean setDataSource(Context context, MediaPlayer mp, int resid) {
    try {
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
        if (afd == null)
            return false;
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        return true;
    } catch (IOException ex) {
        Log.e(TAG, "create failed:", ex);
    // fall through
    } catch (IllegalArgumentException ex) {
        Log.e(TAG, "create failed:", ex);
    // fall through
    } catch (SecurityException ex) {
        Log.e(TAG, "create failed:", ex);
    // fall through
    }
    return false;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException)

Aggregations

AssetFileDescriptor (android.content.res.AssetFileDescriptor)262 IOException (java.io.IOException)147 ParcelFileDescriptor (android.os.ParcelFileDescriptor)57 FileNotFoundException (java.io.FileNotFoundException)56 MediaPlayer (android.media.MediaPlayer)44 ContentResolver (android.content.ContentResolver)36 Test (org.junit.Test)31 Uri (android.net.Uri)30 FileInputStream (java.io.FileInputStream)30 InputStream (java.io.InputStream)23 RemoteException (android.os.RemoteException)22 File (java.io.File)21 Bundle (android.os.Bundle)16 FileDescriptor (java.io.FileDescriptor)16 Nullable (android.annotation.Nullable)15 Bitmap (android.graphics.Bitmap)15 Parcel (android.os.Parcel)14 Resources (android.content.res.Resources)13 BitmapFactory (android.graphics.BitmapFactory)13 DeadObjectException (android.os.DeadObjectException)12