Search in sources :

Example 36 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 37 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 38 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 39 with AssetFileDescriptor

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

the class ContentProviderProxy method openAssetFile.

public AssetFileDescriptor openAssetFile(String callingPkg, Uri url, String mode) throws RemoteException, FileNotFoundException {
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    try {
        data.writeInterfaceToken(IContentProvider.descriptor);
        data.writeString(callingPkg);
        url.writeToParcel(data, 0);
        data.writeString(mode);
        mRemote.transact(IContentProvider.OPEN_ASSET_FILE_TRANSACTION, data, reply, 0);
        DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(reply);
        int has = reply.readInt();
        AssetFileDescriptor fd = has != 0 ? AssetFileDescriptor.CREATOR.createFromParcel(reply) : null;
        return fd;
    } finally {
        data.recycle();
        reply.recycle();
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Parcel(android.os.Parcel)

Example 40 with AssetFileDescriptor

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

the class SoundPool method load.

/**
     * Load the sound from the specified APK resource.
     *
     * Note that the extension is dropped. For example, if you want to load
     * a sound from the raw resource file "explosion.mp3", you would specify
     * "R.raw.explosion" as the resource ID. Note that this means you cannot
     * have both an "explosion.wav" and an "explosion.mp3" in the res/raw
     * directory.
     * 
     * @param context the application context
     * @param resId the resource ID
     * @param priority the priority of the sound. Currently has no effect. Use
     *                 a value of 1 for future compatibility.
     * @return a sound ID. This value can be used to play or unload the sound.
     */
public int load(Context context, int resId, int priority) {
    AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
    int id = 0;
    if (afd != null) {
        id = _load(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength(), priority);
        try {
            afd.close();
        } catch (java.io.IOException ex) {
        //Log.d(TAG, "close failed:", ex);
        }
    }
    return id;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException)

Aggregations

AssetFileDescriptor (android.content.res.AssetFileDescriptor)189 IOException (java.io.IOException)100 ParcelFileDescriptor (android.os.ParcelFileDescriptor)49 FileNotFoundException (java.io.FileNotFoundException)41 MediaPlayer (android.media.MediaPlayer)28 Test (org.junit.Test)28 ContentResolver (android.content.ContentResolver)26 RemoteException (android.os.RemoteException)19 File (java.io.File)18 FileInputStream (java.io.FileInputStream)17 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 InputStream (java.io.InputStream)12 Resources (android.content.res.Resources)11 Uri (android.net.Uri)11 FileOutputStream (java.io.FileOutputStream)11 Bundle (android.os.Bundle)10