Search in sources :

Example 66 with AssetFileDescriptor

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

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 67 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project xDrip-plus by jamorham.

the class AlertPlayer method setMediaDataSource.

// from resource id
private boolean setMediaDataSource(Context context, MediaPlayer mp, int resid) {
    try {
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
        if (afd == null)
            return false;
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        return true;
    } catch (IOException | NullPointerException | IllegalArgumentException | SecurityException ex) {
        Log.e(TAG, "setMediaDataSource from resource id failed:", ex);
    }
    return false;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException)

Example 68 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project xDrip-plus by jamorham.

the class AlertPlayer method setDataSource.

private boolean setDataSource(Context context, MediaPlayer mp, int resid) {
    try {
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
        if (afd == null)
            return false;
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        return true;
    } catch (IOException ex) {
        Log.e(TAG, "create failed:", ex);
    // fall through
    } catch (IllegalArgumentException ex) {
        Log.e(TAG, "create failed:", ex);
    // fall through
    } catch (SecurityException ex) {
        Log.e(TAG, "create failed:", ex);
    // fall through
    }
    return false;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException)

Example 69 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project SmartMesh_Android by SmartMeshFoundation.

the class BeepManager method buildMediaPlayer.

private static MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    // When the beep has finished playing, rewind to queue up another one.
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer player) {
            player.seekTo(0);
        }
    });
    AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.baidu_beep);
    try {
        mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        file.close();
        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
        mediaPlayer.prepare();
    } catch (IOException ioe) {
        Log.w(TAG, ioe);
        mediaPlayer = null;
    }
    return mediaPlayer;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 70 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project subsampling-scale-image-view by davemorrissey.

the class SkiaPooledImageRegionDecoder method initialiseDecoder.

/**
 * Initialises a new {@link BitmapRegionDecoder} and adds it to the pool, unless the pool has
 * been recycled while it was created.
 */
private void initialiseDecoder() throws Exception {
    String uriString = uri.toString();
    BitmapRegionDecoder decoder;
    long fileLength = Long.MAX_VALUE;
    if (uriString.startsWith(RESOURCE_PREFIX)) {
        Resources res;
        String packageName = uri.getAuthority();
        if (context.getPackageName().equals(packageName)) {
            res = context.getResources();
        } else {
            PackageManager pm = context.getPackageManager();
            res = pm.getResourcesForApplication(packageName);
        }
        int id = 0;
        List<String> segments = uri.getPathSegments();
        int size = segments.size();
        if (size == 2 && segments.get(0).equals("drawable")) {
            String resName = segments.get(1);
            id = res.getIdentifier(resName, "drawable", packageName);
        } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) {
            try {
                id = Integer.parseInt(segments.get(0));
            } catch (NumberFormatException ignored) {
            }
        }
        try {
            AssetFileDescriptor descriptor = context.getResources().openRawResourceFd(id);
            fileLength = descriptor.getLength();
        } catch (Exception e) {
        // Pooling disabled
        }
        decoder = BitmapRegionDecoder.newInstance(context.getResources().openRawResource(id), false);
    } else if (uriString.startsWith(ASSET_PREFIX)) {
        String assetName = uriString.substring(ASSET_PREFIX.length());
        try {
            AssetFileDescriptor descriptor = context.getAssets().openFd(assetName);
            fileLength = descriptor.getLength();
        } catch (Exception e) {
        // Pooling disabled
        }
        decoder = BitmapRegionDecoder.newInstance(context.getAssets().open(assetName, AssetManager.ACCESS_RANDOM), false);
    } else if (uriString.startsWith(FILE_PREFIX)) {
        decoder = BitmapRegionDecoder.newInstance(uriString.substring(FILE_PREFIX.length()), false);
        try {
            File file = new File(uriString);
            if (file.exists()) {
                fileLength = file.length();
            }
        } catch (Exception e) {
        // Pooling disabled
        }
    } else {
        InputStream inputStream = null;
        try {
            ContentResolver contentResolver = context.getContentResolver();
            inputStream = contentResolver.openInputStream(uri);
            decoder = BitmapRegionDecoder.newInstance(inputStream, false);
            try {
                AssetFileDescriptor descriptor = contentResolver.openAssetFileDescriptor(uri, "r");
                if (descriptor != null) {
                    fileLength = descriptor.getLength();
                }
            } catch (Exception e) {
            // Stick with MAX_LENGTH
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                /* Ignore */
                }
            }
        }
    }
    this.fileLength = fileLength;
    this.imageDimensions.set(decoder.getWidth(), decoder.getHeight());
    decoderLock.writeLock().lock();
    try {
        if (decoderPool != null) {
            decoderPool.add(decoder);
        }
    } finally {
        decoderLock.writeLock().unlock();
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) InputStream(java.io.InputStream) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder) Point(android.graphics.Point) ContentResolver(android.content.ContentResolver) PackageManager(android.content.pm.PackageManager) Resources(android.content.res.Resources) File(java.io.File)

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