Search in sources :

Example 96 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project LuaViewSDK by alibaba.

the class UDAudio method play.

/**
 * start playing audio
 *
 * @param uriOrName
 * @param loopTimes
 * @return
 */
public synchronized UDAudio play(String uriOrName, Integer loopTimes) {
    stopAndReset();
    if (uriOrName != null && uriOrName.equals(this.mUriOrName) == false) {
        // url 不同
        this.mUriOrName = uriOrName;
    }
    if (loopTimes != null) {
        this.mLoopTimes = loopTimes;
    }
    if (this.mUriOrName != null) {
        final MediaPlayer player = getMediaPlayer();
        if (player != null && player.isPlaying() == false) {
            String uri = null;
            boolean assetFileExist = false;
            if (URLUtil.isNetworkUrl(this.mUriOrName) || URLUtil.isFileUrl(this.mUriOrName) || URLUtil.isAssetUrl(this.mUriOrName)) {
                // net & file & asset
                uri = this.mUriOrName;
            } else {
                // plain text, use as file path
                uri = getLuaResourceFinder().buildFullPathInBundleOrAssets(this.mUriOrName);
                assetFileExist = AssetUtil.exists(getContext(), uri);
            }
            try {
                if (assetFileExist) {
                    final AssetFileDescriptor descriptor = getContext().getAssets().openFd(uri);
                    player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
                } else {
                    player.setDataSource(uri);
                }
                player.setOnErrorListener(this);
                player.setOnCompletionListener(this);
                player.setOnPreparedListener(this);
                player.setLooping((this.mLoopTimes != null && this.mLoopTimes > 1) ? true : false);
                player.prepareAsync();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return this;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) MediaPlayer(android.media.MediaPlayer)

Example 97 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project u2020 by JakeWharton.

the class MockRequestHandler method load.

@Override
public Result load(Request request, int networkPolicy) throws IOException {
    // Grab only the path sans leading slash.
    String imagePath = request.uri.getPath().substring(1);
    // Check the disk cache for the image. A non-null return value indicates a hit.
    boolean cacheHit = emulatedDiskCache.get(imagePath) != null;
    // If there's a hit, grab the image stream and return it.
    if (cacheHit) {
        return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.DISK);
    }
    // If we are not allowed to hit the network and the cache missed return a big fat nothing.
    if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
        return null;
    }
    // to fake an network error.
    if (behavior.calculateIsFailure()) {
        SystemClock.sleep(behavior.calculateDelay(MILLISECONDS));
        throw new IOException("Fake network error!");
    }
    // We aren't throwing a network error so fake a round trip delay.
    SystemClock.sleep(behavior.calculateDelay(MILLISECONDS));
    // Since we cache missed put it in the LRU.
    AssetFileDescriptor fileDescriptor = assetManager.openFd(imagePath);
    long size = fileDescriptor.getLength();
    fileDescriptor.close();
    emulatedDiskCache.put(imagePath, size);
    // Grab the image stream and return it.
    return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.NETWORK);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException)

Example 98 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project NotificationPeekPort by lzanita09.

the class ContactHelper method openDisplayPhoto.

/**
 * Get the InputStream object of the contact photo with given contact ID.
 *
 * @param context       Context object of the caller.
 * @param contactId     Contact ID.
 * @return              InputStream object of the contact photo.
 */
public static InputStream openDisplayPhoto(Context context, long contactId) {
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
    try {
        AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
        return fd.createInputStream();
    } catch (IOException e) {
        return null;
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) Uri(android.net.Uri)

Example 99 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project aFileChooser by iPaulPro.

the class LocalStorageProvider method openDocumentThumbnail.

@Override
public AssetFileDescriptor openDocumentThumbnail(final String documentId, final Point sizeHint, final CancellationSignal signal) throws FileNotFoundException {
    // Assume documentId points to an image file. Build a thumbnail no
    // larger than twice the sizeHint
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(documentId, options);
    final int targetHeight = 2 * sizeHint.y;
    final int targetWidth = 2 * sizeHint.x;
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inSampleSize = 1;
    if (height > targetHeight || width > targetWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        // height and width larger than the requested height and width.
        while ((halfHeight / options.inSampleSize) > targetHeight || (halfWidth / options.inSampleSize) > targetWidth) {
            options.inSampleSize *= 2;
        }
    }
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(documentId, options);
    // Write out the thumbnail to a temporary file
    File tempFile = null;
    FileOutputStream out = null;
    try {
        tempFile = File.createTempFile("thumbnail", null, getContext().getCacheDir());
        out = new FileOutputStream(tempFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (IOException e) {
        Log.e(LocalStorageProvider.class.getSimpleName(), "Error writing thumbnail", e);
        return null;
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
                Log.e(LocalStorageProvider.class.getSimpleName(), "Error closing thumbnail", e);
            }
    }
    // AssetFileDescriptor
    return new AssetFileDescriptor(ParcelFileDescriptor.open(tempFile, ParcelFileDescriptor.MODE_READ_ONLY), 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}
Also used : Bitmap(android.graphics.Bitmap) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileOutputStream(java.io.FileOutputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) File(java.io.File) Point(android.graphics.Point)

Example 100 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project ABPlayer by winkstu.

the class MediaPlayerDemo_Audio method createMediaPlayer.

public MediaPlayer createMediaPlayer(Context context, int resid) {
    try {
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
        MediaPlayer mp = new MediaPlayer(context);
        mp.setDataSource(afd.getFileDescriptor());
        afd.close();
        mp.prepare();
        return mp;
    } catch (IOException ex) {
        Log.d(TAG, "create failed:", ex);
    // fall through
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "create failed:", ex);
    // fall through
    } catch (SecurityException ex) {
        Log.d(TAG, "create failed:", ex);
    // fall through
    }
    return null;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(io.vov.vitamio.MediaPlayer)

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