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();
}
}
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations