Search in sources :

Example 46 with AssetFileDescriptor

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

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)

Example 47 with AssetFileDescriptor

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

the class Ringtone method playFallbackRingtone.

private boolean playFallbackRingtone() {
    if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) {
        int ringtoneType = RingtoneManager.getDefaultType(mUri);
        if (ringtoneType == -1 || RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType) != null) {
            // Default ringtone, try fallback ringtone.
            try {
                AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(com.android.internal.R.raw.fallbackring);
                if (afd != null) {
                    mLocalPlayer = new MediaPlayer();
                    if (afd.getDeclaredLength() < 0) {
                        mLocalPlayer.setDataSource(afd.getFileDescriptor());
                    } else {
                        mLocalPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                    }
                    mLocalPlayer.setAudioAttributes(mAudioAttributes);
                    synchronized (mPlaybackSettingsLock) {
                        applyPlaybackProperties_sync();
                    }
                    mLocalPlayer.prepare();
                    startLocalPlayer();
                    afd.close();
                    return true;
                } else {
                    Log.e(TAG, "Could not load fallback ringtone");
                }
            } catch (IOException ioe) {
                destroyLocalPlayer();
                Log.e(TAG, "Failed to open fallback ringtone");
            } catch (NotFoundException nfe) {
                Log.e(TAG, "Fallback ringtone does not exist");
            }
        } else {
            Log.w(TAG, "not playing fallback for " + mUri);
        }
    }
    return false;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) NotFoundException(android.content.res.Resources.NotFoundException) IOException(java.io.IOException)

Example 48 with AssetFileDescriptor

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

the class CopyJob method copyFileHelper.

/**
     * Handles copying a single file.
     *
     * @param src Info of the file to copy from.
     * @param dest Info of the *file* to copy to. Must be created beforehand.
     * @param destParent Info of the parent of the destination.
     * @param mimeType Mime type for the target. Can be different than source for virtual files.
     * @throws ResourceException
     */
private void copyFileHelper(DocumentInfo src, DocumentInfo dest, DocumentInfo destParent, String mimeType) throws ResourceException {
    CancellationSignal canceller = new CancellationSignal();
    AssetFileDescriptor srcFileAsAsset = null;
    ParcelFileDescriptor srcFile = null;
    ParcelFileDescriptor dstFile = null;
    InputStream in = null;
    ParcelFileDescriptor.AutoCloseOutputStream out = null;
    boolean success = false;
    try {
        // as such format.
        if (src.isVirtualDocument()) {
            try {
                srcFileAsAsset = getClient(src).openTypedAssetFileDescriptor(src.derivedUri, mimeType, null, canceller);
            } catch (FileNotFoundException | RemoteException | RuntimeException e) {
                throw new ResourceException("Failed to open a file as asset for %s due to an " + "exception.", src.derivedUri, e);
            }
            srcFile = srcFileAsAsset.getParcelFileDescriptor();
            try {
                in = new AssetFileDescriptor.AutoCloseInputStream(srcFileAsAsset);
            } catch (IOException e) {
                throw new ResourceException("Failed to open a file input stream for %s due " + "an exception.", src.derivedUri, e);
            }
        } else {
            try {
                srcFile = getClient(src).openFile(src.derivedUri, "r", canceller);
            } catch (FileNotFoundException | RemoteException | RuntimeException e) {
                throw new ResourceException("Failed to open a file for %s due to an exception.", src.derivedUri, e);
            }
            in = new ParcelFileDescriptor.AutoCloseInputStream(srcFile);
        }
        try {
            dstFile = getClient(dest).openFile(dest.derivedUri, "w", canceller);
        } catch (FileNotFoundException | RemoteException | RuntimeException e) {
            throw new ResourceException("Failed to open the destination file %s for writing " + "due to an exception.", dest.derivedUri, e);
        }
        out = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile);
        byte[] buffer = new byte[32 * 1024];
        int len;
        try {
            while ((len = in.read(buffer)) != -1) {
                if (isCanceled()) {
                    if (DEBUG)
                        Log.d(TAG, "Canceled copy mid-copy of: " + src.derivedUri);
                    return;
                }
                out.write(buffer, 0, len);
                makeCopyProgress(len);
            }
            // Need to invoke IoUtils.close explicitly to avoid from ignoring errors at flush.
            IoUtils.close(dstFile.getFileDescriptor());
            srcFile.checkError();
        } catch (IOException e) {
            throw new ResourceException("Failed to copy bytes from %s to %s due to an IO exception.", src.derivedUri, dest.derivedUri, e);
        }
        if (src.isVirtualDocument()) {
            convertedFiles.add(src);
        }
        success = true;
    } finally {
        if (!success) {
            if (dstFile != null) {
                try {
                    dstFile.closeWithError("Error copying bytes.");
                } catch (IOException closeError) {
                    Log.w(TAG, "Error closing destination.", closeError);
                }
            }
            if (DEBUG)
                Log.d(TAG, "Cleaning up failed operation leftovers.");
            canceller.cancel();
            try {
                deleteDocument(dest, destParent);
            } catch (ResourceException e) {
                Log.w(TAG, "Failed to cleanup after copy error: " + src.derivedUri, e);
            }
        }
        // This also ensures the file descriptors are closed.
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(out);
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) RemoteException(android.os.RemoteException) CancellationSignal(android.os.CancellationSignal)

Example 49 with AssetFileDescriptor

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

the class ContentProviderProxy method openTypedAssetFile.

@Override
public AssetFileDescriptor openTypedAssetFile(String callingPkg, Uri url, String mimeType, Bundle opts, ICancellationSignal signal) 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(mimeType);
        data.writeBundle(opts);
        data.writeStrongBinder(signal != null ? signal.asBinder() : null);
        mRemote.transact(IContentProvider.OPEN_TYPED_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 50 with AssetFileDescriptor

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

the class ContentProviderProxy method openAssetFile.

@Override
public AssetFileDescriptor openAssetFile(String callingPkg, Uri url, String mode, ICancellationSignal signal) 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);
        data.writeStrongBinder(signal != null ? signal.asBinder() : null);
        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)

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