Search in sources :

Example 16 with AssetFileDescriptor

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

the class MediaExtractor method setDataSource.

/**
     * Sets the data source as a content Uri.
     *
     * @param context the Context to use when resolving the Uri
     * @param uri the Content URI of the data you want to extract from.
     * @param headers the headers to be sent together with the request for the data
     */
public final void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException {
    String scheme = uri.getScheme();
    if (scheme == null || scheme.equals("file")) {
        setDataSource(uri.getPath());
        return;
    }
    AssetFileDescriptor fd = null;
    try {
        ContentResolver resolver = context.getContentResolver();
        fd = resolver.openAssetFileDescriptor(uri, "r");
        if (fd == null) {
            return;
        }
        // a full file.
        if (fd.getDeclaredLength() < 0) {
            setDataSource(fd.getFileDescriptor());
        } else {
            setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
        }
        return;
    } catch (SecurityException ex) {
    } catch (IOException ex) {
    } finally {
        if (fd != null) {
            fd.close();
        }
    }
    setDataSource(uri.toString(), headers);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) ContentResolver(android.content.ContentResolver)

Example 17 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project Signal-Android by WhisperSystems.

the class MultiDeviceContactUpdateJob method getAvatar.

private Optional<SignalServiceAttachmentStream> getAvatar(@Nullable Uri uri) throws IOException {
    if (uri == null) {
        return Optional.absent();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        try {
            Uri displayPhotoUri = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
            AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
            return Optional.of(SignalServiceAttachment.newStreamBuilder().withStream(fd.createInputStream()).withContentType("image/*").withLength(fd.getLength()).build());
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    Uri photoUri = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    if (photoUri == null) {
        return Optional.absent();
    }
    Cursor cursor = context.getContentResolver().query(photoUri, new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO, ContactsContract.CommonDataKinds.Phone.MIMETYPE }, null, null, null);
    try {
        if (cursor != null && cursor.moveToNext()) {
            byte[] data = cursor.getBlob(0);
            if (data != null) {
                return Optional.of(SignalServiceAttachment.newStreamBuilder().withStream(new ByteArrayInputStream(data)).withContentType("image/*").withLength(data.length).build());
            }
        }
        return Optional.absent();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 18 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 19 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project jmonkeyengine by jMonkeyEngine.

the class NativeVorbisLoader method loadStream.

private static AudioStream loadStream(AssetInfo assetInfo) throws IOException {
    AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo;
    AssetFileDescriptor afd = null;
    NativeVorbisFile file = null;
    boolean success = false;
    try {
        afd = aai.openFileDescriptor();
        int fd = afd.getParcelFileDescriptor().getFd();
        file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength());
        AudioStream stream = new AudioStream();
        stream.setupFormat(file.channels, 16, file.sampleRate);
        stream.updateData(new VorbisInputStream(afd, file), file.duration);
        success = true;
        return stream;
    } finally {
        if (!success) {
            if (file != null) {
                file.close();
            }
            if (afd != null) {
                afd.close();
            }
        }
    }
}
Also used : AudioStream(com.jme3.audio.AudioStream) AssetFileDescriptor(android.content.res.AssetFileDescriptor) AndroidAssetInfo(com.jme3.asset.plugins.AndroidLocator.AndroidAssetInfo)

Example 20 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)

Aggregations

AssetFileDescriptor (android.content.res.AssetFileDescriptor)187 IOException (java.io.IOException)98 ParcelFileDescriptor (android.os.ParcelFileDescriptor)49 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)28 MediaPlayer (android.media.MediaPlayer)27 ContentResolver (android.content.ContentResolver)26 RemoteException (android.os.RemoteException)19 File (java.io.File)18 FileInputStream (java.io.FileInputStream)16 Nullable (android.annotation.Nullable)15 Parcel (android.os.Parcel)14 Bitmap (android.graphics.Bitmap)13 FileDescriptor (java.io.FileDescriptor)13 DeadObjectException (android.os.DeadObjectException)12 Resources (android.content.res.Resources)11 FileOutputStream (java.io.FileOutputStream)11 InputStream (java.io.InputStream)11 Uri (android.net.Uri)10 Bundle (android.os.Bundle)10