Search in sources :

Example 91 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project android-zxingLibrary by yipianfengye.

the class CaptureFragment method initBeepSound.

private void initBeepSound() {
    if (playBeep && mediaPlayer == null) {
        // The volume on STREAM_SYSTEM is not adjustable, and users found it
        // too loud,
        // so we now play on the music stream.
        getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnCompletionListener(beepListener);
        AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
            file.close();
            mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
            mediaPlayer.prepare();
        } catch (IOException e) {
            mediaPlayer = null;
        }
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 92 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project robolectric by robolectric.

the class ShadowBitmapRegionDecoderTest method testNewInstance.

@Test
public void testNewInstance() throws Exception {
    assertThat(BitmapRegionDecoder.newInstance(ByteStreams.toByteArray(getImageInputStream()), 0, 0, false)).isNotNull();
    try (AssetFileDescriptor afd = getImageFd()) {
        assertThat(BitmapRegionDecoder.newInstance(afd.getFileDescriptor(), false)).isNotNull();
    }
    try (InputStream inputStream = getImageInputStream()) {
        assertThat(BitmapRegionDecoder.newInstance(inputStream, false)).isNotNull();
    }
    assertThat(BitmapRegionDecoder.newInstance(getGeneratedImageFile(), false)).isNotNull();
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 93 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project robolectric by robolectric.

the class ShadowMediaPlayerTest method testSetDataSourceAssetFileDescriptorDataSource.

@Config(minSdk = N)
@Test
public void testSetDataSourceAssetFileDescriptorDataSource() throws IOException {
    Application context = ApplicationProvider.getApplicationContext();
    try (AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.drawable.an_image)) {
        DataSource ds = toDataSource(fd);
        ShadowMediaPlayer.addMediaInfo(ds, info);
        mediaPlayer.setDataSource(fd);
        assertWithMessage("dataSource").that(shadowMediaPlayer.getDataSource()).isEqualTo(ds);
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Application(android.app.Application) DataSource(org.robolectric.shadows.util.DataSource) MediaDataSource(android.media.MediaDataSource) DataSource.toDataSource(org.robolectric.shadows.util.DataSource.toDataSource) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 94 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project robolectric by robolectric.

the class ShadowLegacyAssetManager method openFd.

@Implementation
protected final AssetFileDescriptor openFd(String fileName) throws IOException {
    Path path = findAssetFile(fileName);
    if (path.getFileSystem().provider().getScheme().equals("jar")) {
        path = getFileFromZip(path);
    }
    ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(path.toFile(), ParcelFileDescriptor.MODE_READ_ONLY);
    return new AssetFileDescriptor(parcelFileDescriptor, 0, Files.size(path));
}
Also used : Path(java.nio.file.Path) AssetFileDescriptor(android.content.res.AssetFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor) Implementation(org.robolectric.annotation.Implementation)

Example 95 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project robolectric by robolectric.

the class ShadowLegacyResourcesImpl method openRawResourceFd.

/**
 * Since {@link AssetFileDescriptor}s are not yet supported by Robolectric, {@code null} will
 * be returned if the resource is found. If the resource cannot be found, {@link Resources.NotFoundException} will
 * be thrown.
 */
@Implementation(maxSdk = M)
public AssetFileDescriptor openRawResourceFd(int id) throws Resources.NotFoundException {
    InputStream inputStream = openRawResource(id);
    if (!(inputStream instanceof FileInputStream)) {
        // todo fixme
        return null;
    }
    FileInputStream fis = (FileInputStream) inputStream;
    try {
        return new AssetFileDescriptor(ParcelFileDescriptor.dup(fis.getFD()), 0, fis.getChannel().size());
    } catch (IOException e) {
        throw newNotFoundException(id);
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Implementation(org.robolectric.annotation.Implementation)

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