use of android.content.res.AssetFileDescriptor in project BlogSource by TeachCourse.
the class BeepManager method buildMediaPlayer.
private MediaPlayer buildMediaPlayer(Context activity) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
try {
AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
return mediaPlayer;
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.release();
return null;
}
}
use of android.content.res.AssetFileDescriptor in project BlogSource by TeachCourse.
the class CaptureActivity method initBeepSound.
private void initBeepSound() {
if (playBeep && mediaPlayer == null) {
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(beepListener);
try {
AssetFileDescriptor fileDescriptor = getAssets().openFd("qrbeep.ogg");
this.mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
this.mediaPlayer.setVolume(0.1F, 0.1F);
this.mediaPlayer.prepare();
} catch (IOException e) {
this.mediaPlayer = null;
}
}
}
use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.
the class StorageProvider method openOrCreateImageThumbnailCleared.
protected AssetFileDescriptor openOrCreateImageThumbnailCleared(long id, CancellationSignal signal) throws FileNotFoundException {
final ContentResolver resolver = getContext().getContentResolver();
ParcelFileDescriptor pfd = openImageThumbnailCleared(id, signal);
if (pfd == null) {
// No thumbnail yet, so generate. This is messy, since we drop the
// Bitmap on the floor, but its the least-complicated way.
final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Images.Thumbnails.getThumbnail(resolver, id, Images.Thumbnails.MINI_KIND, opts);
pfd = openImageThumbnailCleared(id, signal);
}
if (pfd == null) {
// Phoey, fallback to full image
final Uri fullUri = ContentUris.withAppendedId(Images.Media.EXTERNAL_CONTENT_URI, id);
pfd = resolver.openFileDescriptor(fullUri, "r");
}
final int orientation = queryOrientationForImage(id, signal);
final Bundle extras;
if (orientation != 0) {
extras = new Bundle(1);
extras.putInt(DocumentsContract.EXTRA_ORIENTATION, orientation);
} else {
extras = null;
}
return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}
use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.
the class DocumentsContract method openImageThumbnail.
/**
* Open the given image for thumbnail purposes, using any embedded EXIF
* thumbnail if available, and providing orientation hints from the parent
* image.
*
* @hide
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
Bundle extras = null;
if (!Utils.hasKitKat()) {
return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}
try {
final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
case ExifInterface.ORIENTATION_ROTATE_90:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 270);
break;
}
final long[] thumb = exif.getThumbnailRange();
if (thumb != null) {
return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
}
} catch (IOException e) {
CrashReportingManager.logException(e);
}
return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.
the class DocumentsContract method getDocumentThumbnails.
public static Bitmap getDocumentThumbnails(ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal) throws RemoteException, IOException {
final Bundle openOpts = new Bundle();
openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size);
AssetFileDescriptor afd = null;
Bitmap bitmap = null;
try {
// TODO : Remove
// afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal);
afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts);
final FileDescriptor fd = afd.getFileDescriptor();
final long offset = afd.getStartOffset();
// Try seeking on the returned FD, since it gives us the most
// optimal decode path; otherwise fall back to buffering.
BufferedInputStream is = null;
try {
OsCompat.lseek(fd, offset, OsCompat.SEEK_SET);
} catch (Exception e) {
is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE);
is.mark(THUMBNAIL_BUFFER_SIZE);
}
// We requested a rough thumbnail size, but the remote size may have
// returned something giant, so defensively scale down as needed.
final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
if (is != null) {
BitmapFactory.decodeStream(is, null, opts);
} else {
BitmapFactory.decodeFileDescriptor(fd, null, opts);
}
final int widthSample = opts.outWidth / size.x;
final int heightSample = opts.outHeight / size.y;
opts.inJustDecodeBounds = false;
opts.inSampleSize = Math.min(widthSample, heightSample);
if (is != null) {
is.reset();
bitmap = BitmapFactory.decodeStream(is, null, opts);
} else {
try {
OsCompat.lseek(fd, offset, OsCompat.SEEK_SET);
} catch (OsCompat.ExecutionFailedException e) {
throw new IOException(e);
}
bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts);
}
// Transform the bitmap if requested. We use a side-channel to
// communicate the orientation, since EXIF thumbnails don't contain
// the rotation flags of the original image.
Bundle extras = null;
if (Utils.hasKitKat()) {
extras = afd.getExtras();
}
final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
if (orientation != 0) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final Matrix m = new Matrix();
m.setRotate(orientation, width / 2, height / 2);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
}
} finally {
IoUtils.closeQuietly(afd);
}
return bitmap;
}
Aggregations