/** * Opens a stream to the given URI. * @return Never returns null. * @throws Throws an InvalidArgumentException for relative URIs. Relative URIs should be * resolved before being passed into this function. * @throws Throws an IOException if the URI cannot be opened. */ public OutputStream openOutputStream(Uri uri, boolean append) throws IOException { assertBackgroundThread(); switch (getUriType(uri)) { case URI_TYPE_FILE: { File localFile = new File(uri.getPath()); File parent = localFile.getParentFile(); if (parent != null) { parent.mkdirs(); } return new FileOutputStream(localFile, append); } case URI_TYPE_CONTENT: case URI_TYPE_RESOURCE: { AssetFileDescriptor assetFd = contentResolver.openAssetFileDescriptor(uri, append ? "wa" : "w"); return assetFd.createOutputStream(); } } throw new FileNotFoundException("URI not supported by CordovaResourceApi: " + uri); }
private MediaPlayer buildMediaPlayer(Context activity) { 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(0.10f, 0.10f); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { mediaPlayer.release(); return null; } }
@Test public void testLoadResource_returnsFileDescriptor() throws Exception { Context context = RuntimeEnvironment.application; Uri uri = Uri.parse("file://nothing"); ContentResolver contentResolver = context.getContentResolver(); ContentResolverShadow shadow = (ContentResolverShadow) Shadow.extract(contentResolver); AssetFileDescriptor assetFileDescriptor = mock(AssetFileDescriptor.class); ParcelFileDescriptor parcelFileDescriptor = mock(ParcelFileDescriptor.class); when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor); shadow.registerFileDescriptor(uri, assetFileDescriptor); FileDescriptorLocalUriFetcher fetcher = new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri); fetcher.loadData(Priority.NORMAL, callback); verify(callback).onDataReady(eq(parcelFileDescriptor)); }
protected AssetFileDescriptor openOrCreateVideoThumbnailCleared( long id, CancellationSignal signal) throws FileNotFoundException { final ContentResolver resolver = getContext().getContentResolver(); AssetFileDescriptor afd = openVideoThumbnailCleared(id, signal); if (afd == 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; Video.Thumbnails.getThumbnail(resolver, id, Video.Thumbnails.MINI_KIND, opts); afd = openVideoThumbnailCleared(id, signal); } return afd; }
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() { public void onCompletion(MediaPlayer player) { player.seekTo(0); } }); AssetFileDescriptor file = activity.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 ioe) { Log.w(TAG, ioe); mediaPlayer = null; } return mediaPlayer; }
private void enableMediaPlayer() { mediaPlayer = new MediaPlayer(); float volume = prefs.getFloat(PreferenceConstants.BELL_VOLUME, PreferenceConstants.DEFAULT_BELL_VOLUME); mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); AssetFileDescriptor file = res.openRawResourceFd(R.raw.bell); try { mediaPlayer.setLooping(false); mediaPlayer.setDataSource(file.getFileDescriptor(), file .getStartOffset(), file.getLength()); file.close(); mediaPlayer.setVolume(volume, volume); mediaPlayer.prepare(); } catch (IOException e) { Log.e(TAG, "Error setting up bell media player", e); } }
@Test public void testLoadResource_returnsFileDescriptor() throws Exception { Context context = RuntimeEnvironment.application; Uri uri = Uri.parse("file://nothing"); ContentResolver contentResolver = context.getContentResolver(); ContentResolverShadow shadow = Shadow.extract(contentResolver); AssetFileDescriptor assetFileDescriptor = mock(AssetFileDescriptor.class); ParcelFileDescriptor parcelFileDescriptor = mock(ParcelFileDescriptor.class); when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor); shadow.registerFileDescriptor(uri, assetFileDescriptor); FileDescriptorLocalUriFetcher fetcher = new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri); fetcher.loadData(Priority.NORMAL, callback); verify(callback).onDataReady(eq(parcelFileDescriptor)); }
protected AssetFileDescriptor openVideoThumbnailCleared(long id, CancellationSignal signal) throws FileNotFoundException { final ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = null; try { cursor = resolver.query(Video.Thumbnails.EXTERNAL_CONTENT_URI, VideoThumbnailQuery.PROJECTION, Video.Thumbnails.VIDEO_ID + "=" + id, null, null); if (cursor.moveToFirst()) { final String data = cursor.getString(VideoThumbnailQuery._DATA); return new AssetFileDescriptor(ParcelFileDescriptor.open( new File(data), ParcelFileDescriptor.MODE_READ_ONLY), 0, AssetFileDescriptor.UNKNOWN_LENGTH); } } finally { IoUtils.closeQuietly(cursor); } return null; }
private int getLength(ImageRequest imageRequest) { AssetFileDescriptor fd = null; try { fd = mResources.openRawResourceFd(getResourceId(imageRequest)); return (int) fd.getLength(); } catch (Resources.NotFoundException e) { return -1; } finally { try { if (fd != null) { fd.close(); } } catch (IOException ignored) { // There's nothing we can do with the exception when closing descriptor. } } }
private int getLength(ImageRequest imageRequest) { AssetFileDescriptor fd = null; try { fd = mAssetManager.openFd(getAssetName(imageRequest)); return (int) fd.getLength(); } catch (IOException e) { return -1; } finally { try { if (fd != null) { fd.close(); } } catch (IOException ignored) { // There's nothing we can do with the exception when closing descriptor. } } }
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); AssetFileDescriptor file = activity.getResources().openRawResourceFd(keepass2android.plugin.qr.R.raw.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; }
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setOnErrorListener(this); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setLooping(false); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
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; } }
/** * 播放音频文件 * * @param file * @param repeat */ private void playAssetsFile(String file, boolean repeat) { try { Log.i(TAG, "file:" + file); currentFile = file; AssetFileDescriptor fd = mAssetManager.openFd(file); mPlayer.reset(); mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength()); mPlayer.setAudioStreamType(mediator.isBlueToothHeadSet() ? AudioManager.STREAM_VOICE_CALL : AudioManager.STREAM_MUSIC); mPlayer.setLooping(repeat); mPlayer.prepareAsync(); fd.close(); } catch (Exception e) { e.printStackTrace(); } }
private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; AssetFileDescriptor fileDescriptor = null; try { fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r"); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor( fileDescriptor.getFileDescriptor(), null, options); Timber.d(options.inSampleSize + " sample method bitmap ... " + actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight()); return actuallyUsableBitmap; }
@Override public void onSurfaceCreated(SurfaceHolder holder) { L.d("VideoEngine#onSurfaceCreated "); super.onSurfaceCreated(holder); mMediaPlayer = new MediaPlayer(); mMediaPlayer.setSurface(holder.getSurface()); try { AssetManager assetMg = getApplicationContext().getAssets(); AssetFileDescriptor fileDescriptor = assetMg.openFd("test1.mp4"); mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength()); mMediaPlayer.setLooping(true); mMediaPlayer.setVolume(0, 0); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }
private final AssetFileDescriptor openTypedAssetFileImpl( Uri uri, String mimeTypeFilter, Bundle opts, CancellationSignal signal) throws FileNotFoundException { enforceTree(uri); final String documentId = getDocumentId(uri); if (opts != null && opts.containsKey(ContentResolver.EXTRA_SIZE)) { final Point sizeHint = opts.getParcelable(ContentResolver.EXTRA_SIZE); return openDocumentThumbnail(documentId, sizeHint, signal); } if ("*/*".equals(mimeTypeFilter)) { // If they can take anything, the untyped open call is good enough. return openAssetFile(uri, "r"); } final String baseType = getType(uri); if (baseType != null && ClipDescription.compareMimeTypes(baseType, mimeTypeFilter)) { // Use old untyped open call if this provider has a type for this // URI and it matches the request. return openAssetFile(uri, "r"); } // For any other yet unhandled case, let the provider subclass handle it. return openTypedDocument(documentId, mimeTypeFilter, opts, signal); }
private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; AssetFileDescriptor fileDescriptor = null; try { fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r"); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor( fileDescriptor.getFileDescriptor(), null, options); Log.d(TAG, options.inSampleSize + " sample method bitmap ... " + actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight()); return actuallyUsableBitmap; }
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; } }
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(this.rawBeep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setOnErrorListener(this); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setLooping(false); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
private void initBeepSound() { this.playBeep = true; if (((AudioManager) getSystemService("audio")).getRingerMode() != 2) { this.playBeep = false; } if (this.playBeep && this.mediaPlayer == null) { setVolumeControlStream(3); this.mediaPlayer = new MediaPlayer(); this.mediaPlayer.setAudioStreamType(3); this.mediaPlayer.setOnCompletionListener(this.beepListener); AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep); try { this.mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); file.close(); this.mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); this.mediaPlayer.prepare(); } catch (IOException e) { this.mediaPlayer = null; } } }
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.qr_sacn); 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; } }
/** * find path of an asset from a mediaPath using case-insensitive comparison, * return AssetFileDescriptor of that asset * Throws IOException if there is no matching path * @param form the Form * @param mediaPath the path to the media */ private static AssetFileDescriptor getAssetsIgnoreCaseAfd(Form form, String mediaPath) throws IOException{ try { return form.getAssets().openFd(mediaPath); } catch (IOException e) { String path = findCaseinsensitivePath(form, mediaPath); if (path == null){ throw e; } else { return form.getAssets().openFd(path); } } }
@Override public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { initIfNecessary(); String path = uri.getEncodedPath(); if ( path.startsWith("/") ) { path = path.substring(1); } return mAPKExtensionFile.getAssetFileDescriptor(path); }
@Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { initIfNecessary(); AssetFileDescriptor af = openAssetFile(uri, mode); if ( null != af ) { return af.getParcelFileDescriptor(); } return null; }
@Override public AssetFileDescriptor openDocumentThumbnail( String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException { // TODO: extend ExifInterface to support fds final ParcelFileDescriptor pfd = openDocument(docId, "r", signal); return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH); }