Search in sources :

Example 71 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project BlogSource by TeachCourse.

the class BeepManager method buildMediaPlayer.

private MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    try {
        AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        } finally {
            file.close();
        }
        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
        mediaPlayer.prepare();
        return mediaPlayer;
    } catch (IOException ioe) {
        Log.w(TAG, ioe);
        mediaPlayer.release();
        return null;
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 72 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project BlogSource by TeachCourse.

the class CaptureActivity method initBeepSound.

private void initBeepSound() {
    if (playBeep && mediaPlayer == null) {
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnCompletionListener(beepListener);
        try {
            AssetFileDescriptor fileDescriptor = getAssets().openFd("qrbeep.ogg");
            this.mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
            this.mediaPlayer.setVolume(0.1F, 0.1F);
            this.mediaPlayer.prepare();
        } catch (IOException e) {
            this.mediaPlayer = null;
        }
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 73 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.

the class StorageProvider method openOrCreateImageThumbnailCleared.

protected AssetFileDescriptor openOrCreateImageThumbnailCleared(long id, CancellationSignal signal) throws FileNotFoundException {
    final ContentResolver resolver = getContext().getContentResolver();
    ParcelFileDescriptor pfd = openImageThumbnailCleared(id, signal);
    if (pfd == null) {
        // No thumbnail yet, so generate. This is messy, since we drop the
        // Bitmap on the floor, but its the least-complicated way.
        final BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Images.Thumbnails.getThumbnail(resolver, id, Images.Thumbnails.MINI_KIND, opts);
        pfd = openImageThumbnailCleared(id, signal);
    }
    if (pfd == null) {
        // Phoey, fallback to full image
        final Uri fullUri = ContentUris.withAppendedId(Images.Media.EXTERNAL_CONTENT_URI, id);
        pfd = resolver.openFileDescriptor(fullUri, "r");
    }
    final int orientation = queryOrientationForImage(id, signal);
    final Bundle extras;
    if (orientation != 0) {
        extras = new Bundle(1);
        extras.putInt(DocumentsContract.EXTRA_ORIENTATION, orientation);
    } else {
        extras = null;
    }
    return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) ParcelFileDescriptor(android.os.ParcelFileDescriptor) BitmapFactory(android.graphics.BitmapFactory) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 74 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.

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
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
    final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle extras = null;
    if (!Utils.hasKitKat()) {
        return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
    }
    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) {
        CrashReportingManager.logException(e);
    }
    return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) ExifInterface(android.support.media.ExifInterface) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) TargetApi(android.annotation.TargetApi)

Example 75 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.

the class DocumentsContract method getDocumentThumbnails.

public static Bitmap getDocumentThumbnails(ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal) throws RemoteException, IOException {
    final Bundle openOpts = new Bundle();
    openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size);
    AssetFileDescriptor afd = null;
    Bitmap bitmap = null;
    try {
        // TODO : Remove
        // afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal);
        afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts);
        final FileDescriptor fd = afd.getFileDescriptor();
        final long offset = afd.getStartOffset();
        // Try seeking on the returned FD, since it gives us the most
        // optimal decode path; otherwise fall back to buffering.
        BufferedInputStream is = null;
        try {
            OsCompat.lseek(fd, offset, OsCompat.SEEK_SET);
        } catch (Exception e) {
            is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE);
            is.mark(THUMBNAIL_BUFFER_SIZE);
        }
        // We requested a rough thumbnail size, but the remote size may have
        // returned something giant, so defensively scale down as needed.
        final BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        if (is != null) {
            BitmapFactory.decodeStream(is, null, opts);
        } else {
            BitmapFactory.decodeFileDescriptor(fd, null, opts);
        }
        final int widthSample = opts.outWidth / size.x;
        final int heightSample = opts.outHeight / size.y;
        opts.inJustDecodeBounds = false;
        opts.inSampleSize = Math.min(widthSample, heightSample);
        if (is != null) {
            is.reset();
            bitmap = BitmapFactory.decodeStream(is, null, opts);
        } else {
            try {
                OsCompat.lseek(fd, offset, OsCompat.SEEK_SET);
            } catch (OsCompat.ExecutionFailedException e) {
                throw new IOException(e);
            }
            bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts);
        }
        // Transform the bitmap if requested. We use a side-channel to
        // communicate the orientation, since EXIF thumbnails don't contain
        // the rotation flags of the original image.
        Bundle extras = null;
        if (Utils.hasKitKat()) {
            extras = afd.getExtras();
        }
        final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
        if (orientation != 0) {
            final int width = bitmap.getWidth();
            final int height = bitmap.getHeight();
            final Matrix m = new Matrix();
            m.setRotate(orientation, width / 2, height / 2);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
        }
    } finally {
        IoUtils.closeQuietly(afd);
    }
    return bitmap;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) OsCompat(dev.dworks.apps.anexplorer.misc.OsCompat) IOException(java.io.IOException) AssetFileDescriptor(android.content.res.AssetFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) RemoteException(android.os.RemoteException) OperationCanceledException(android.os.OperationCanceledException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) Point(android.graphics.Point) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) BufferedInputStream(java.io.BufferedInputStream) BitmapFactory(android.graphics.BitmapFactory)

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